MemberInfo中的常量
本文关键字:常量 MemberInfo | 更新日期: 2023-09-27 18:12:54
我试图在我的类Car中获取有关常量的信息,但不幸的是,结果我只得到4个字段(名称,currentSpeed, maxSpeed,状态)。如何获取字段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);
这应该给你常量,在你的查询BindingFlags.Instance
是消除静态的,常量是static
。
type.GetFields(BindingFlags.Static |
BindingFlags.FlattenHierarchy)
.Where(fi => fi.IsLiteral && !fi.IsInitOnly).ToList();