如何通过反射从基本类型获取静态属性

发布于 2025-02-03 21:02:18 字数 744 浏览 3 评论 0 原文

我尝试通过反射从基本类型中获取静态属性。在这方面,有关此主题有很多问题,但它们都专注于从目标类型中获取基本类型或静态属性的非静态属性。

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);
    }
}

输出:

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

这似乎仅适应目标类型的静态特性以及目标类型和基本类型的非静态特性。但是我只想要基本类型的静态属性( static_baseprop

有人知道该怎么做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心旳酸楚 2025-02-10 21:02:18

要仅获取基本类型的静态属性,我建议访问 type.basetype 属性(如公共|静态

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();
}

免责声明:这仅适用于继承层次结构的一个级别。如果您想要更深入的见解,则需要深入研究基质。

输出:

system.string static_baseprop

如果您想要整个继承层次结构中的所有静态属性,则可以使用这种bindingfalgs的组合:

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

如果您在 a 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();
}

输出:

system.string static_prop
System.String static_baseprop
system.string static_parentbaseprop

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文