【问题标题】:Difference between GetGetMethod method and GetMethod property?GetGetMethod 方法和 GetMethod 属性之间的区别?
【发布时间】:2013-05-12 09:20:19
【问题描述】:

.Net 4.5 将 PropertyInfo.GetMethod 作为 PropertyInfo 类的属性。它与PropertyInfo.GetGetMethod 方法有什么不同吗?文档页面几乎是空白的。我能找到的唯一区别是GetGetMethod 默认只返回公共getter,而GetMethod 甚至返回非公共getter(GetGetMethod(true) 也是如此)。

.NET 4.5 中同样有GetSetMethod 方法和SetMethod 属性。为什么在 .NET 中引入它?

【问题讨论】:

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


    【解决方案1】:

    没有区别。属性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这个词!

    【讨论】:

    • +1 因为The property GetMethod calls GetGetMethod to get the getter.这句话让我发笑
    猜你喜欢
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2012-10-03
    • 2011-06-22
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多