MemberInfo中的常量

时间:2014-07-15 11:08:09

标签: c# constants memberinfo

我尝试在我的班级Car中获取有关常量的信息,但不幸的是在结果中我只得到4个字段(name,currentSpeed,maxSpeed,state)。我怎样才能得到字段const。

感谢。

public abstract class Car
{
    protected string name;
    protected short currentSpeed;
    protected short maxSpeed;
    protected EngineState state;

    protected const int const = 1;
 }

............................................... .........

 Type type = assembly.GetType("CarLibrary.Car"); 

 MemberInfo[] members = type.GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

 foreach (MemberInfo element in members)
 Console.WriteLine("{0,-15}:  {1}", element.MemberType, element);

1 个答案:

答案 0 :(得分:1)

这应该为您提供常量,在您的查询BindingFlags.Instance中消除静态,常量为static

type.GetFields(BindingFlags.Static |
               BindingFlags.FlattenHierarchy)
    .Where(fi => fi.IsLiteral && !fi.IsInitOnly).ToList();