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

后端 未结 9 1121
[愿得一人]
[愿得一人] 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:40

    Here's a modified solution based on the implementation given in this blog post:

    var explicitProperties =
        from prop in typeof(TempClass).GetProperties(
            BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
        let getAccessor = prop.GetGetMethod(true)
        where getAccessor.IsFinal && getAccessor.IsPrivate
        select prop;
    
    foreach (var p in explicitProperties)
        Console.WriteLine(p.Name);
    

提交回复
热议问题