gpt4 book ai didi

c# - PropertyInfo 上的 GetSetMethod 和 SetMethod 有什么区别?

转载 作者:行者123 更新时间:2023-11-30 19:17:04 29 4
gpt4 key购买 nike

所以,PropertyInfo 有一个 GetSetMethod 方法,它返回这个属性的 setter 方法。它还有一个 SetMethod 属性(据我所知)。

我问这个是因为如果属性不是公共(public)的而 SetMethod 仍然有效,GetSetMethod 似乎返回 null。

我在 MSDN 上找不到太多内容。

最佳答案

你是对的。

来自 mscorlib(刚刚使用 dotPeek):

    /// <summary>
/// Returns the public set accessor for this property.
/// </summary>
///
/// <returns>
/// The MethodInfo object representing the Set method for this property if the set accessor is public, or null if the set accessor is not public.
/// </returns>
[__DynamicallyInvokable]
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public MethodInfo GetSetMethod()
{
return this.GetSetMethod(false);
}

/// <summary>
/// When overridden in a derived class, returns the set accessor for this property.
/// </summary>
///
/// <returns>
/// Value Condition A <see cref="T:System.Reflection.MethodInfo"/> object representing the Set method for this property. The set accessor is public.-or- <paramref name="nonPublic"/> is true and the set accessor is non-public. null<paramref name="nonPublic"/> is true, but the property is read-only.-or- <paramref name="nonPublic"/> is false and the set accessor is non-public.-or- There is no set accessor.
/// </returns>
/// <param name="nonPublic">Indicates whether the accessor should be returned if it is non-public. true if a non-public accessor is to be returned; otherwise, false. </param><exception cref="T:System.Security.SecurityException">The requested method is non-public and the caller does not have <see cref="T:System.Security.Permissions.ReflectionPermission"/> to reflect on this non-public method. </exception>
[__DynamicallyInvokable]
public abstract MethodInfo GetSetMethod(bool nonPublic);

[__DynamicallyInvokable]
public virtual MethodInfo SetMethod
{
[__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get
{
return this.GetSetMethod(true);
}
}

关于c# - PropertyInfo 上的 GetSetMethod 和 SetMethod 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19691072/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com