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
  • 20,203
  • 28
  • 86
  • 118

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
  • 225,310
  • 48
  • 427
  • 736
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,262
  • 3
  • 37
  • 64
1

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

Dewfy
  • 23,277
  • 13
  • 73
  • 121
-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,215
  • 1
  • 23
  • 42