2010-02-11 94 views

回答

3

通過反映企業的性質集的迭代,你可以通過檢查所有屬性

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

+0

完美答案:代碼片段和輸出。謝謝! – Lernkurve 2010-02-11 12:09:28

-1

字典包含KeyValuePair所以這樣的事情應該工作

foreach (KeyValuePair kvp in System.Windows.SystemParameters) 
{ 
    var myvalue = kvp.value; 
} 

話雖如此MSDN上的幫助,沒有提及System.Windows.SystemParameters是一個字典

+0

。 ..但是,SystemParameters不是一個字典,所以這是行不通的。 – 2010-02-11 09:33:48

4

不幸的是什麼,SystemParameters沒有按不會實現任何類型的Enumerable接口,因此C#中的標準迭代編碼習慣用法都不會像這樣工作。

但是,您可以使用反射獲得類的所有公共靜態屬性:

var props = typeof(SystemParameters) 
    .GetProperties(BindingFlags.Public | BindingFlags.Static); 

然後,您可以在props迭代。

+0

感謝有關SystemParameters沒有實現任何種類的Enumerable接口的提示! – Lernkurve 2010-02-11 12:11:06

1

可能是更好的方式使用反射使用Type.GetProperties