2

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.

Community
  • 1
  • 1
Lernkurve
  • 18,816
  • 25
  • 81
  • 114

4 Answers4

4

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
  • 218,019
  • 46
  • 414
  • 706
3

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
  • 9,162
  • 3
  • 36
  • 63
1

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

Dewfy
  • 22,648
  • 12
  • 68
  • 116
-1

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
  • 5,115
  • 1
  • 22
  • 42