Question

.Net 4.5 has PropertyInfo.GetMethod as a property on PropertyInfo class. Is it doing anything different from PropertyInfo.GetGetMethod method? The documentation page is virtually blank. The only difference I can find is GetGetMethod by default returns only public getter while GetMethod returns even non-public getter (the same is achieved by GetGetMethod(true)).

Similarly there is GetSetMethod method and SetMethod property in .NET 4.5. Why was it introduced in .NET?

Était-ce utile?

La solution

There is no difference. The property GetMethod calls GetGetMethod to get the getter. 1 Here's what ILSpy tells me about the property implementation:

// 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);
    }
}

The property GetMethod is simply easier to use, because it does not relate on an parameter.

1 Never thought I could use the word get so many times in just one sentence!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top