反映 .net 中的常量属性/字段
我有一个类,如下所示:
public class MyConstants
{
public const int ONE = 1;
public const int TWO = 2;
Type thisObject;
public MyConstants()
{
thisObject = this.GetType();
}
public void EnumerateConstants()
{
PropertyInfo[] thisObjectProperties = thisObject.GetProperties(BindingFlags.Public);
foreach (PropertyInfo info in thisObjectProperties)
{
//need code to find out of the property is a constant
}
}
}
基本上它试图反映自身。我知道如何反映字段 ONE, &二。但我怎么知道它是否是一个常数呢?
I have a class which looks like as follows:
public class MyConstants
{
public const int ONE = 1;
public const int TWO = 2;
Type thisObject;
public MyConstants()
{
thisObject = this.GetType();
}
public void EnumerateConstants()
{
PropertyInfo[] thisObjectProperties = thisObject.GetProperties(BindingFlags.Public);
foreach (PropertyInfo info in thisObjectProperties)
{
//need code to find out of the property is a constant
}
}
}
Bascially it is trying to reflect itself. I know how to reflect fields ONE, & TWO. But how do I know if it is a constant or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那是因为它们是字段,而不是属性。尝试:
编辑:DataDink是对的,使用IsLiteral更流畅
That's because they're fields, not properties. Try:
Edit: DataDink's right, it's smoother to use IsLiteral
FieldInfo 对象实际上有大量的“IsSomething”布尔值:
无论如何,这可以为您节省少量检查属性的代码。
FieldInfo objects actually have a ton of "IsSomething" booleans right on them:
Which saves you a tiny ammount of code over checking the attributes anyways.