如何使用反射獲取任何類型的所有常量?如何通過反射獲取某個類型的所有常量?
回答
雖然這是一箇舊的代碼:
private FieldInfo[] GetConstants(System.Type type)
{
ArrayList constants = new ArrayList();
FieldInfo[] fieldInfos = type.GetFields(
// Gets all public and static fields
BindingFlags.Public | BindingFlags.Static |
// This tells it to get the fields from all base types as well
BindingFlags.FlattenHierarchy);
// Go through the list and only pick out the constants
foreach(FieldInfo fi in fieldInfos)
// IsLiteral determines if its value is written at
// compile time and not changeable
// IsInitOnly determine if the field can be set
// in the body of the constructor
// for C# a field which is readonly keyword would have both true
// but a const field would have only IsLiteral equal to true
if(fi.IsLiteral && !fi.IsInitOnly)
constants.Add(fi);
// Return an array of FieldInfos
return (FieldInfo[])constants.ToArray(typeof(FieldInfo));
}
您可以輕鬆地將其轉換爲更清晰的代碼使用泛型和LINQ:
private List<FieldInfo> GetConstants(Type type)
{
FieldInfo[] fieldInfos = type.GetFields(BindingFlags.Public |
BindingFlags.Static | BindingFlags.FlattenHierarchy);
return fieldInfos.Where(fi => fi.IsLiteral && !fi.IsInitOnly).ToList();
}
或用一條線:
type.GetFields(BindingFlags.Public | BindingFlags.Static |
BindingFlags.FlattenHierarchy)
.Where(fi => fi.IsLiteral && !fi.IsInitOnly).ToList();
*我+ 1 *被在我甚至通過第二條線之前......我注意到你正在經歷每一步......以意向爲目的的設計目的......!當人們需要從中吸取教訓時,這很重要。我希望每個有你的經驗的人都能像你這樣做。 – LoneXcoder 2012-12-10 08:49:16
我不確定關於IsLiteral和IsInitOnly的斷言。在測試看來,對於靜態只讀屬性IsLiteral總是假 - 因此IsLiteral是唯一需要檢查以查找常量的標誌,並且您可以忽略IsInitOnly。我嘗試過使用不同的字段類型(例如String,Int32)來查看這是否有所作爲,但沒有。 – 2015-05-05 09:55:02
此外,要從FieldInfo獲取const的值,請使用GetRawConstantValue()。 – 2015-07-07 00:15:48
使用property.GetConstantValue()來獲得價值
當你擁有房產時,情況可能就是這樣 - 但是,你是如何獲得房產的? – 2015-12-14 10:15:11
在.Net 4.5中它是:'GetRawConstantValue()' – Chris 2016-03-11 20:16:03
爲類型的擴展名:
public static class TypeExtensions
{
public static IEnumerable<FieldInfo> GetConstants(this Type type)
{
var fieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
return fieldInfos.Where(fi => fi.IsLiteral && !fi.IsInitOnly);
}
public static IEnumerable<T> GetConstantsValues<T>(this Type type) where T : class
{
var fieldInfos = GetConstants(type);
return fieldInfos.Select(fi => fi.GetRawConstantValue() as T);
}
}
如果你想獲得的值的特定類型的所有常量,從目標類型,在這裏是一個擴展方法(此頁面上延伸一些問題的答案):
public static class TypeUtilities
{
public static List<T> GetAllPublicConstantValues<T>(this Type type)
{
return type
.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
.Where(fi => fi.IsLiteral && !fi.IsInitOnly && fi.FieldType == typeof(T))
.Select(x => (T)x.GetRawConstantValue())
.ToList();
}
}
然後,對於一類這樣
static class MyFruitKeys
{
public const string Apple = "apple";
public const string Plum = "plum";
public const string Peach = "peach";
public const int WillNotBeIncluded = -1;
}
您可以獲取string
常數值如下:
List<string> result = typeof(MyFruitKeys).GetAllPublicConstantValues<string>();
//result[0] == "apple"
//result[1] == "plum"
//result[2] == "peach"
- 1. 如何通過反射來獲取類中某個屬性的「類類型」?
- 2. ObservableArrayList:如何通過反射獲取通用類型?
- 3. 如何通過反射獲取EntityReference類型的PropertyInfo.PropertyType名稱
- 4. 如何通過反射獲取集合中包含的類型
- 5. 如何通過反射獲取枚舉值的所有描述?
- 6. 如何通過反射獲取由Ruby的Module類定義的常量?
- 7. 通過反射獲取.Net對應類型的C#類型
- 8. 通過反射獲取所有內部類
- 9. 如何通過反射獲取委託類型的返回類型?
- 10. 如何獲取包中所有類通用的常量變量?
- 11. 如何通過反射獲得通用參數的類型
- 12. 無法通過反射獲取泛型類型的屬性
- 13. 如何通過.GetType獲取類常量()
- 14. 通過反射獲取子類字段
- 15. 在運行時通過反射獲取通用字段類型
- 16. 如何通過jQuery遍歷某個類型的所有元素?
- 17. 通過反射獲取XmlRootAttribute.ElementName
- 18. 如何通過反射獲得成員的類型?
- 19. 獲取數組類型元素的(通過反射)
- 20. 反射從DLL中的所有類型獲取值
- 21. 如何通過反射來獲取和使用類/實體類型?
- 22. 有沒有辦法通過反射來獲取類型的別名?
- 23. 如何獲取某個城市中某個類型的所有Google地點?
- 24. 獲取方法通過反射返回類型
- 25. .net通過反射獲取枚舉類型
- 26. 通過反射在mscorlib 2.0.5.0(又名Silverlight mscorlib)中獲取類型?
- 27. 如何通過反射屬性獲取變量/字段名稱?
- 28. 如何通過反射從引用的程序集中獲取類型
- 29. 使用反射獲取所有Integer數據類型字段
- 30. 使用反射來獲取所有屬性並通過方法
http://weblogs.asp.net/whaggard/archive/2003/02/20/2708.aspx – 2012-04-21 18:38:44