反映在.net中不变的属性/领域 [英] Reflecting constant properties/fields in .net
本文介绍了反映在.net中不变的属性/领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
<一href="http://stackoverflow.com/questions/1287797/type-getfields-only-returning-public-const-fields">Type.GetFields() - 只有在返回&ldquo;公共常量&rdquo;的田
Possible Duplicate:
Type.GetFields() - only returning “public const” fields
我有一个类,它看上去就像下面这样:
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它试图反映出来。我知道如何反映油田中的一个,和放大器;二。但我怎么知道这是否是一个常数或不?
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?
推荐答案
这是因为他们的字段,而不是属性。尝试:
That's because they're fields, not properties. Try:
public void EnumerateConstants() {
FieldInfo[] thisObjectProperties = thisObject.GetFields();
foreach (FieldInfo info in thisObjectProperties) {
if (info.IsLiteral) {
//Constant
}
}
}
编辑:DataDink的权利,它的顺畅使用IsLiteral
DataDink's right, it's smoother to use IsLiteral
这篇关于反映在.net中不变的属性/领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文