I'm writing an app that uses reflection to get each property name and its value from the class System.Windows.Forms.SystemInformation
my current code is a snippet from this thread:
How can you loop over the properties of a class?
Marc's answer is probably the best but it is too complicated for me atm since it is my first time doing reflections and his skills are too high.
So this is what I got first.
foreach (PropertyInfo prop in typeof(System.Windows.Forms.SystemInformation).GetProperties())
{
richTextBox1.AppendText(prop.Name + "\t\t" + prop.GetValue(null, null)
}
but I don't know how to loop over the attributes of the class powerstatus
.
I thought about checking if the current prop is a primitive type.
If it isn't I would call the upperfunction recursively.
So it looks like this:
private void readProperties(Type T, int indent)
{
//var x = System.Windows.Forms.SystemInformation.ActiveWindowTrackingDelay;
foreach (PropertyInfo prop in T.GetProperties())
{
for (int x = 0; x < indent; x++)
richTextBox1.AppendText("\t");
richTextBox1.AppendText(prop.Name + "\t\t" + prop.GetValue(null, null) +"\n");
if (!prop.PropertyType.IsPrimitive)
readProperties(prop.PropertyType, indent+1);
//System.Windows.Forms.PowerStatus PS = new PowerStatus();
}
}
But now I get the exception: "Die nicht-statische Methode erfordert ein Ziel" translated something like: "The non-static method needs a target"
The exception is thrown when the function is being called recursively the first time.
The attribute is primaryMonitorSize which is typeof Size
. imho it has something to do with the fact that I'am parsing the type Size
and not System.Windows.Forms.SystemInformation.primaryMonitorSize
so that I know the actual type but not of which member of my program it is because it could also be the size of the winForm.
So how can I fix this? I appreciate every constructive criticism.
@Edit: this is a msdn example. But it doesn't look pretty. http://msdn.microsoft.com/de-de/library/system.windows.forms.systeminformation.powerstatus.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2