如何迭代System.Windows.SystemParameters
並輸出所有的鍵和值?如何遍歷System.Windows.SystemParameters?
我發現What is the best way to iterate over a Dictionary in C#?,但不知道如何修改SystemParameters
的代碼。也許你也可以解釋我本人是如何能夠自己想出來的;可能通過使用Reflector。
如何迭代System.Windows.SystemParameters
並輸出所有的鍵和值?如何遍歷System.Windows.SystemParameters?
我發現What is the best way to iterate over a Dictionary in C#?,但不知道如何修改SystemParameters
的代碼。也許你也可以解釋我本人是如何能夠自己想出來的;可能通過使用Reflector。
通過反映企業的性質集的迭代,你可以通過檢查所有屬性
var result = new Dictionary<string, object>();
var type = typeof (System.Windows.SystemParameters);
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Static);
foreach(var property in properties)
{
result.Add(property.Name, property.GetValue(null, null));
}
foreach(var pair in result)
{
Console.WriteLine("{0} : {1}", pair.Key, pair.Value);
}
這將產生以下輸出創建字典.. 。
FocusBorderWidth:1
FocusBorderHeight:1
高對比度:假
FocusBorderWidthKey:FocusBorderWidth
FocusBorderHeightKey:FocusBorderHeight
HighContrastKey:高對比度
陰影效果:真
FlatMenu:真
WORKAREA:0,0,1681,1021
DropShadowKey:陰影效果
FlatMenuKey:FlatMenu
字典包含KeyValuePair所以這樣的事情應該工作
foreach (KeyValuePair kvp in System.Windows.SystemParameters)
{
var myvalue = kvp.value;
}
話雖如此MSDN上的幫助,沒有提及System.Windows.SystemParameters是一個字典
。 ..但是,SystemParameters不是一個字典,所以這是行不通的。 – 2010-02-11 09:33:48
不幸的是什麼,SystemParameters沒有按不會實現任何類型的Enumerable接口,因此C#中的標準迭代編碼習慣用法都不會像這樣工作。
但是,您可以使用反射獲得類的所有公共靜態屬性:
var props = typeof(SystemParameters)
.GetProperties(BindingFlags.Public | BindingFlags.Static);
然後,您可以在props
迭代。
感謝有關SystemParameters沒有實現任何種類的Enumerable接口的提示! – Lernkurve 2010-02-11 12:11:06
可能是更好的方式使用反射使用Type.GetProperties
完美答案:代碼片段和輸出。謝謝! – Lernkurve 2010-02-11 12:09:28