C# - 从静态类获取静态属性的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1487867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:01:12  来源:igfitidea点击:

C# - Get values of static properties from static class

c#reflectionclassstaticproperties

提问by JamesW

I'm trying to loop through some static properties in a simple static class in order to populate a combo box with their values, but am having difficulties.

我试图在一个简单的静态类中遍历一些静态属性,以便用它们的值填充组合框,但遇到了困难。

Here is the simple class:

这是简单的类:

public static MyStaticClass()
{
    public static string property1 = "NumberOne";
    public static string property2 = "NumberTwo";
    public static string property3 = "NumberThree";
}

... and the code attempting to retrieve the values:

...以及尝试检索值的代码:

Type myType = typeof(MyStaticClass);
PropertyInfo[] properties = myType.GetProperties(
       BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach (PropertyInfo property in properties)
{
    MyComboBox.Items.Add(property.GetValue(myType, null).ToString());
}

If I don't supply any binding flags then I get about 57 properties including things like System.Reflection.Module Module and all sorts of other inherited things I don't care about. My 3 declared properties are not present.

如果我不提供任何绑定标志,那么我会得到大约 57 个属性,包括 System.Reflection.Module Module 之类的东西以及我不关心的各种其他继承的东西。我的 3 个声明属性不存在。

If I supply various combinations of other flags then it always returns 0 properties. Great.

如果我提供其他标志的各种组合,那么它总是返回 0 属性。伟大的。

Does it matter that my static class is actually declared within another non-static class?

我的静态类实际上是在另一个非静态类中声明的重要吗?

What am I doing wrong?

我究竟做错了什么?

采纳答案by M4N

The problem is that property1..3are not properties, but fields.

问题是这property1..3不是属性,而是字段。

To make them properties change them to:

要使它们的属性更改为:

private static string _property1 = "NumberOne";
public static string property1
{
  get { return _property1; }
  set { _property1 = value; }
}

Or use auto properties and initialize their values in the static constructor of the class:

或者使用自动属性并在类的静态构造函数中初始化它们的值:

public static string property1 { get; set; }

static MyStaticClass()
{
  property1 = "NumberOne";
}

...or use myType.GetFields(...)if fields are what you want to use.

...或者使用myType.GetFields(...)if 字段是您要使用的内容。

回答by Nick Berardi

Try removing BindingFlags.DeclaredOnly, because according to MSDN:

尝试删除BindingFlags.DeclaredOnly,因为根据 MSDN:

Specifies that only members declared at the level of the supplied type's hierarchy should be considered. Inherited members are not considered.

指定只应考虑在提供的类型层次结构级别声明的成员。不考虑继承成员。

Since static's cannot be inherited, this might be causing your issues. Also I noticed the fields you are trying to get are not properties. So try using

由于无法继承静态,这可能会导致您的问题。我还注意到您尝试获取的字段不是属性。所以尝试使用

type.GetFields(...)