0

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

谢谢。

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);
4

1 回答 1

1

这应该给你常量,在你的查询BindingFlags.Instance中消除了静态,常量是static.

type.GetFields(BindingFlags.Static |
               BindingFlags.FlattenHierarchy)
    .Where(fi => fi.IsLiteral && !fi.IsInitOnly).ToList();
于 2014-07-15T11:11:37.817 回答