.net - GetGetMethod 方法和 GetMethod 属性之间的区别?

标签 .net .net-4.5 getter-setter accessor propertyinfo

.Net 4.5 有 PropertyInfo.GetMethod作为 PropertyInfo 类的属性。它与PropertyInfo.GetGetMethod 方法有什么不同吗?文档页面几乎是空白的。我能发现的唯一区别是 GetGetMethod 默认情况下仅返回公共(public) getter,而 GetMethod 甚至返回非公共(public) getter(通过 GetGetMethod(true)< 实现相同的效果)/)。

类似地,.NET 4.5 中还有 GetSetMethod 方法和 SetMethod 属性。为什么在 .NET 中引入它?

最佳答案

没有什么区别。属性GetMethod 调用GetGetMethod 来获取getter。 1 以下是 ILSpy 告诉我的有关属性实现的信息:

// System.Reflection.PropertyInfo
[__DynamicallyInvokable]
public virtual MethodInfo GetMethod
{
    [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    get
    {
        return this.GetGetMethod(true);
    }
}

属性 GetMethod 更易于使用,因为它与参数无关。

1 从来没有想过我可以在一个句子中多次使用get这个词!

关于.net - GetGetMethod 方法和 GetMethod 属性之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16505890/

相关文章:

java - 无法解决maven项目eclipse photon中的错误

c# - 对 Type.GetMethod() 的重复调用是否会在内部缓存?

c# - .Net 是否有 org.apache.commons.lang.StringEscapeUtils 的实现?

c# - EditorFor 为 byte 和 short 呈现不同

c# - ODP Oracle.DataAccess 使用 .NETFramework 4.5 但不使用 .NET Framework 4.0

javadoc - 我可以避免在 getter/setter 方法中重复字段的 Javadoc 注释吗?

c# - 使用 Expression<Func<T>> 使值类型表现为引用类型

c# - 带虚线的 TreeView/TreeViewItem ControlTemplates

c# - Entity Framework 5 枚举命名

java - 在Java中编写使用多维数组的基本方法