tags:

views:

81

answers:

4

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; maybe by using Reflector.

A: 

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

Andrew
...but, SystemParameters is not a Dictionary, so this will not work.
Mark Seemann
+4  A: 

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.

Mark Seemann
Thanks for the hint about SystemParameters not implementing any kind of Enumerable interface!
Lernkurve
+1  A: 

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

Dewfy
+3  A: 

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

Rohan West
Perfect answer: code snippet and output. Thanks!
Lernkurve