开发者

How to iterate over System.Windows.SystemParameters?

How can I iterate over System.Windows.SystemParameters and output all keys and values?

I've found What is the best way to iterate over a Dictionary in C#?, but don't know how to adapt the code for SystemParameters.

Perhaps you could also explain how I could have figured it out by myself; 开发者_StackOverflow中文版maybe by using Reflector.


Unfortunately, SystemParameters doesn't implement any kind of Enumerable interface, so none of the standard iteration coding idioms in C# will work just like that.

However, you can use Reflection to get all the public static properties of the class:

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

You can then iterate over props.


using reflection you can create a dictionary by inspecting all the properties

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);
}

This will produce the following output...

FocusBorderWidth : 1
FocusBorderHeight : 1
HighContrast : False
FocusBorderWidthKey : FocusBorderWidth
FocusBorderHeightKey : FocusBorderHeight
HighContrastKey : HighContrast
DropShadow : True
FlatMenu : True
WorkArea : 0,0,1681,1021
DropShadowKey : DropShadow
FlatMenuKey : FlatMenu


May be better way to iterate through reflected set of proprties using Type.GetProperties


A dictionary contains a KeyValuePair so something like this should work

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

Having said that the help on msdn does not mention anything about System.Windows.SystemParameters being a dictionary

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜