Search code examples
c#reflectionexplicit-interface

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


More specifically, if I have:

public class TempClass : TempInterface
{

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

    public int TempProperty
    {
        get;
        set;
    }
}

public interface TempInterface
{
    int TempProperty
    {
        get;
        set;
    }
    int TempProperty2
    {
        get;
        set;
    }
}

How do I use reflection to get all the propertyInfos for properties explicitly implementing TempInterface?

Thanks.


Solution

  • I had to modify Jacob Carpenter's answer but it works nicely. nobugz's also works but Jacobs is more compact.

    var explicitProperties =
    from method in typeof(TempClass).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
    where method.IsFinal && method.IsPrivate
    select method;