Reputation: 1459
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
Upvotes: 0
Views: 859
Reputation: 942187
private void readProperties(Type T, int indent)
You will need to improve this method, it isn't suitable to read the properties of the PowerStatus property. Which requires an object, you cannot pass null to the GetValue() method. So write it like this instead:
private void readProperties(Type T, int indent, object obj) {
//...
var value = prop.GetValue(obj, null);
if (prop.PropertyType.IsPrimive) {
richTextBox1.AppendText(prop.Name + "\t\t" + value.ToString() +"\n");
}
else {
richTextBox1.AppendText(prop.Name + ":\n");
readProperties(prop.PropertyType, indent+1, value);
}
}
Note how it now reads the property value and if it is an object then it passes that object to readProperties again so GetValue() will work correctly. Pass null to kick this off for SystemInformation.
Upvotes: 3
Reputation: 32571
All the properties of System.Windows.Forms.SystemInformation
are static, that's why you don't have to pass an object and the
The non-static method needs a target
exception does not occur in your first code sample. For the properties which are not static, you need to use a target when you call the GetValue(object obj, object[] index)
method.
Check out this console application an note that in the second foreach
, the GetValue
method actually takes SystemInformation.PowerStatus
(a target instance) as the first parameter:
class Program
{
public static void Main()
{
var type = typeof(System.Windows.Forms.PowerStatus);
foreach (PropertyInfo prop in type.GetProperties(
BindingFlags.Static |
BindingFlags.Public |
BindingFlags.NonPublic))
{
Console.WriteLine(prop.Name + "\t\t"
+ prop.GetValue(null, null));
}
foreach (PropertyInfo prop in type.GetProperties(
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic))
{
Console.WriteLine(prop.Name + "\t\t" + prop.GetValue(
System.Windows.Forms.SystemInformation.PowerStatus, null));
}
}
}
Upvotes: 2