【发布时间】:2010-12-02 01:15:30
【问题描述】:
我正在尝试遍历一个简单静态类中的一些静态属性,以便用它们的值填充组合框,但遇到了困难。
这是一个简单的类:
public static MyStaticClass()
{
public static string property1 = "NumberOne";
public static string property2 = "NumberTwo";
public static string property3 = "NumberThree";
}
...以及尝试检索值的代码:
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());
}
如果我不提供任何绑定标志,那么我会得到大约 57 个属性,包括诸如 System.Reflection.Module Module 之类的东西以及我不关心的各种其他继承的东西。我的 3 个声明的属性不存在。
如果我提供其他标志的各种组合,那么它总是返回 0 个属性。太好了。
我的静态类实际上是在另一个非静态类中声明的吗?
我做错了什么?
【问题讨论】:
-
@Veverke:考虑到 OP 犯了其他人也可能犯的错误,因此保留不正确的术语对于确保 Google 能够找到该帖子至关重要。这样的编辑会破坏问题,因为在您进行编辑后没有留下任何问题。
标签: c# reflection class static properties