You can achieve this by getting PropertyInfo in a declaring property type, a simple extension method can be ...
public static class Extensions { public static MethodInfo GetSetMethodOnDeclaringType(this PropertyInfo propertyInfo) { var methodInfo = propertyInfo.GetSetMethod(true); return methodInfo ?? propertyInfo .DeclaringType .GetProperty(propertyInfo.Name) .GetSetMethod(true); } }
then your calling code ...
class Program { static void Main(string[] args) { MethodInfo setMethod = typeof(Foo) .GetProperty("Prop") .GetSetMethodOnDeclaringType(); if (setMethod == null) Console.WriteLine("NULL"); else Console.WriteLine(setMethod.ToString()); Console.ReadKey(); } }
source share