C# 循环遍历类的常量成员

C# 循环遍历类的常量成员,c#,class,C#,Class,我有一个包含常量字符串的类。我想把所有这些字符串放到下拉式集合中。最好的方法是什么?这就是我现在所拥有的,理论上,我认为这是最好的方法 public class TestClass { private const string _testA = "Test A"; private const string _testB = "Test B"; public string TestA { get { return _testA; } }

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

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;
}
问题是,这会在foreach上返回一个错误:“…不包含GetEnumerator的公共定义。”我尝试创建GetEnumerator,但没有成功,而且过去也没有使用GetEnumerator


非常感谢您的帮助

您可以使用反射循环所有属性:

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;
}
var instance = new TestClass();
foreach(PropertyInfo pi in typeof(TestClass))
{
      var val = pi.GetValue(instance,null);
}

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

public Ienumerable<string> GetStrings(){
   yield return TestA;
   yield return TestB;
}
public Ienumerable GetStrings(){
产量回报试验;
收益率试验b;
}
否则,您应该查看反射以返回静态和字符串属性,然后通过调用它们来获取值


关于GJ

您可以使用反射来循环trought类属性:

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;
}
var instance = new TestClass();
foreach(PropertyInfo pi in typeof(TestClass))
{
      var val = pi.GetValue(instance,null);
}

您需要使用反射从自定义类型获取每个字符串的名称,然后还/可选地获取其中每个字符串的值

大概是这样的:

TestClass theClass = new TestClass();

foreach (PropertyInfo property in theClass.GetType().GetProperties())
{

    Console.WriteLine(property.Name);
    Console.WriteLine(property.GetValue(theClass, null));
}

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

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

fi.GetValue(null)

在循环内部

我也遇到了同样的挑战;获取类的所有常量(不是属性!)。基于最流行的答案(关于属性)和约翰的答案(关于常数),我写了这个。我对它进行了测试,效果很好

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));
        }
    }
private List lstOfConstants=new List();
foreach(typeof(TestClass).GetFields()中的var常量)
{
if(constant.IsLiteral&!constant.IsInitOnly)
{
Add((字符串)constant.GetValue(null));
}
}

对我来说,这没有返回任何东西(如果返回属性,但常量值不是属性,这似乎是正确的),所以我不知道它是如何得到如此多的提升的@John下面的回答适用于常量字符串成员。我与@christutty有相同的问题,它似乎适用于
{get;set;}
的属性,但似乎不适用于已经具有预定值的属性。它获取属性,而不是常量(如问题所要求的),这给了我奇怪的结果。我声明了一个类,该类包含每个内置类型的公共常量,并且该方法总是忽略小数,出于某种原因
public const decimal decimal=1.0m
has IsInitOnly=true事实上,我似乎不是第一个注意到这一点的人:什么是
\u TestA
?OP只有
\u testA
(小写),这是常量本身,而不是它的名称。