.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/