如何迭代System.Windows.SystemParameters?

时间:2010-02-11 09:27:15

标签: c# wpf parameters

如何迭代System.Windows.SystemParameters并输出所有键和值?

我找到What is the best way to iterate over a Dictionary in C#?,但不知道如何调整SystemParameters的代码。

也许你也可以解释一下我怎么能自己解决这个问题;也许可以使用Reflector

4 个答案:

答案 0 :(得分:4)

不幸的是,SystemParameters没有实现任何类型的Enumerable接口,因此C#中的标准迭代编码习惯用法都不会像那样工作。

但是,您可以使用反射来获取该类的所有公共静态属性:

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

然后,您可以迭代props

答案 1 :(得分: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
HighContrast:假的 FocusBorderWidthKey:FocusBorderWidth
FocusBorderHeightKey:FocusBorderHeight
HighContrastKey:HighContrast
DropShadow:真实的 FlatMenu:真的 工作区域:0,0,1681,1021
DropShadowKey:DropShadow
FlatMenuKey:FlatMenu

答案 2 :(得分:1)

可能是使用Type.GetProperties

迭代反映的一组属性的更好方法

答案 3 :(得分:-1)

字典包含KeyValuePair,所以这样的东西应该可以工作

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

说过msdn上的帮助没有提到System.Windows.SystemParameters是字典的任何内容

相关问题