循环通过类的常量成员

div*_*ded 11 c# class

我有一个包含常量字符串的类.我想将所有这些字符串放入下拉集合中.做这个的最好方式是什么?这就是我现在所拥有的,理论上,我认为这将是最好的方法.

public class TestClass
{
    private const string _testA = "Test A";
    private const string _testB = "Test B";

    public string TestA
    {
        get { return _testA; }
    }

    public string TestB
    {
        get { return _testB; }
    }
}

public DropDownItemCollection TestCollection
{
    DropDownItemCollection collection = new DropDownItemCollection();
    TestClass class = new TestClass();

    foreach (string testString in class)
    {
        DropDownItem item = new DropDownItem();
        item.Description = testString;
        item.Value = testString;
        collection.Add(item);
    }

    return collection;
}
Run Code Online (Sandbox Code Playgroud)

问题是这会在foreach上返回一个错误:"...不包含GetEnumerator的公共定义." 我试图创建一个GetEnumerator但是我没有成功,我以前没有使用过GetEnumerator.

任何帮助是极大的赞赏!

Joh*_*ohn 15

有点晚了,但这不是一个更好的解决方案吗?

http://weblogs.asp.net/whaggard/archive/2003/02/20/2708.aspx

private FieldInfo[] GetConstants(System.Type type)
{
    ArrayList constants = new ArrayList();

    FieldInfo[] fieldInfos = type.GetFields(
        // Gets all public and static fields

        BindingFlags.Public | BindingFlags.Static | 
        // This tells it to get the fields from all base types as well

        BindingFlags.FlattenHierarchy);

    // Go through the list and only pick out the constants
    foreach(FieldInfo fi in fieldInfos)
        // IsLiteral determines if its value is written at 
        //   compile time and not changeable
        // IsInitOnly determine if the field can be set 
        //   in the body of the constructor
        // for C# a field which is readonly keyword would have both true 
        //   but a const field would have only IsLiteral equal to true
        if(fi.IsLiteral && !fi.IsInitOnly)
            constants.Add(fi);           

    // Return an array of FieldInfos
    return (FieldInfo[])constants.ToArray(typeof(FieldInfo));
}
Run Code Online (Sandbox Code Playgroud)

如果你需要你可以做的名字

fi.GetValue(null)
Run Code Online (Sandbox Code Playgroud)

在循环内.


gjv*_*amp 8

您可以实现一个产生字符串的方法:

public Ienumerable<string> GetStrings(){
   yield return TestA;
   yield return TestB;
}
Run Code Online (Sandbox Code Playgroud)

否则,您应该查看反射以返回静态和字符串的属性,然后通过调用它们来获取值.

关心GJ


Dar*_*rov 7

您可以使用反射遍历所有属性:

public DropDownItemCollection TestCollection
{
    var collection = new DropDownItemCollection();
    var instance = new TestClass();
    foreach (var prop in typeof(TestClass).GetProperties())
    {
        if (prop.CanRead)
        {
            var value = prop.GetValue(instance, null) as string;
            var item = new DropDownItem();
            item.Description = value;
            item.Value = value;
            collection.Add(item);
        }
    }
    return collection;
}
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,这没有任何回报(如果返回属性似乎是正确的,但const值不是属性)所以我不知道它是如何得到如此多的投票.@ John的答案适用于const字符串成员. (3认同)

pen*_*ner 5

我只是面临同样的挑战;获取我类的所有常量(不是属性!)。基于最受欢迎的答案(针对属性)和John的答案(针对常量),我编写了此代码。我测试了它,效果很好。

private List<string> lstOfConstants= new List<string>();
    foreach (var constant in typeof(TestClass).GetFields())
    {
        if (constant.IsLiteral && !constant.IsInitOnly)
        {
            lstOfConstants.Add((string)constant.GetValue(null));
        }
    }
Run Code Online (Sandbox Code Playgroud)