How do I use reflection to get properties explicitly implementing an interface?

后端 未结 9 1075
[愿得一人]
[愿得一人] 2021-01-03 20:18

More specifically, if I have:

public class TempClass : TempInterface
{

    int TempInterface.TempProperty
    {
        get;
        set;
    }
    int Temp         


        
9条回答
  •  [愿得一人]
    2021-01-03 20:31

    Building on the answer by MrKurt:

    var targetMethods =
        from iface in typeof(TempClass).GetInterfaces()
        from method in typeof(TempClass).GetInterfaceMap(iface).TargetMethods
        select method;
    
    var explicitProps =
        from prop in typeof(TempClass).GetProperties(BindingFlags.Instance |
                                                     BindingFlags.NonPublic)
        where targetMethods.Contains(prop.GetGetMethod(true)) ||
              targetMethods.Contains(prop.GetSetMethod(true))
        select prop;
    

提交回复
热议问题