使用FieldInfo类获取数据成员值

时间:2013-07-17 10:55:33

标签: c# generics collections

class Algo
{
    public const long AlgorithmDescription = 1611048;
    public const long AlgorithmFamilyCodeSequence = 6684719;
    public const long AlgorithmName = 6684726;
    public const long AlgorithmNameCodeSequence = 6684720;
    public const long AlgorithmParameters = 6684722;
    public const long AlgorithmType = 1611047;
    public const long AlgorithmVersion = 6684721;
}
class Class1<T>
{
    SortedList<uint, string> ds;
    public SortedList<uint, string> TraverseTag()
    {
        ds = new SortedList<uint, string>();
        uint i = 0;
        foreach (FieldInfo prop in typeof(Algo).GetFields(BindingFlags.Public |
                                                          BindingFlags.Static | 
                                                          BindingFlags.Instance))
        {
            ds.Add(i, prop.Name);
            i++;
        }
        return ds;
     }
}

public class Program
{
    public static void Main()
    {
        var objClass = new Class1();
        SortedList<uint, string> s = objClass.TraverseTag();
    }
}

Algo类,我为此创建一个dll并使用对Program.cs类的引用并调用TraverseTag()方法,此方法返回uint,而FieldInfo返回给我{{ 1}}但我也希望在{[0,AlgorithmDescription]}类中分配给我的数据成员的值。

例如&gt;

  

{[0,AlgorithmDescription,1611048]}

1 个答案:

答案 0 :(得分:1)

您可以访问FieldInfo实例的GetValue方法。这应该为您提供该字段所属类的特定实例的该字段的值。