Home > Software design >  How to get static properties from base type via reflection
How to get static properties from base type via reflection

Time:06-02

I try to get static properties from a base type via reflection. There are many questions regarding this topic on this side, but they all focus on getting non static properties of a base type or static properties from the target type.

public class A
{
    public static string STATIC_BaseProp => "STATIC_BaseProp"; //<-- I want this
    public string BaseProp => "BaseProp";
}

public class B : A
{
    public static string STATIC_Prop => "STATIC_Prop";
    public string Prop => "PROP";
}

static void Main(string[] args)
{
    var type = typeof(B);
    foreach (var propertyInfo in type.GetProperties())
    {
        Console.log(propertyInfo);
    }
}

Output:

System.String STATIC_Prop
System.String Prop
System.String BaseProp

This seems to only adress the static properties of the target type and the non static properties of the target type and the base type. But I want only the static property of the base type (STATIC_BaseProp)

Does anyone know how to do this?

CodePudding user response:

To get only the static properties of the base type I would suggest to access the Type.BaseType property (like described in this answer) and use only Public | Static Bindingflags:

static void Main(string[] args)
{
    var type = typeof(B);
    foreach (var propertyInfo in type.BaseType.GetProperties( BindingFlags.Public | BindingFlags.Static ))
    {
        Console.WriteLine(propertyInfo);
    }

    Console.ReadKey();
}

disclaimer: this works only for one level of the inheritance hierarchy. You would need to dive through the basetypes if you want deeper insights.

Output:

System.String STATIC_BaseProp

If you want all static properties throughout the entire inheritance hierarchy you can use this combination of BindingFalgs:

type.GetProperties( BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)

Here is an example to demostrate that it works also if you add a parent class on top of A

public class ParentOfA
{
    public static string STATIC_ParentBaseProp => "STATIC_ParentBaseProp"; //<-- I want this
    public string ParentBaseProp => "BaseProp";
}

public class A : ParentOfA
{
    public static string STATIC_BaseProp => "STATIC_BaseProp"; //<-- I want this
    public string BaseProp => "BaseProp";
}

public class B : A
{
    public static string STATIC_Prop => "STATIC_Prop";
    public string Prop => "PROP";
}

static void Main(string[] args)
{
    var type = typeof(B);
    foreach (var propertyInfo in type.GetProperties( 
                                      BindingFlags.Public |
                                      BindingFlags.Static |
                                      BindingFlags.FlattenHierarchy))
    {
        Console.WriteLine(propertyInfo);
    }


    Console.ReadKey();
}

Output:

System.String STATIC_Prop
System.String STATIC_BaseProp
System.String STATIC_ParentBaseProp

  • Related