2013-06-12 19 views
0

我正在写使用反射来获取每个属性名称,并从类其价值的应用System.Windows.Forms.SystemInformation我当前的代码是从这个线程的代码段:C#遍历SystemInformation但与它的属性麻烦电源状态,

How can you loop over the properties of a class?

马克的答案可能是最好的,但对我来说这太复杂了,因为这是我第一次做反思,他的技能太高了。

所以这是我第一次。

foreach (PropertyInfo prop in typeof(System.Windows.Forms.SystemInformation).GetProperties()) 
{ 
    richTextBox1.AppendText(prop.Name + "\t\t" + prop.GetValue(null, null) 
} 

但我不知道如何遍历类powerstatus的属性循环。 我想过检查当前的道具是否是原始类型。 如果不是,我会递归地调用上面的函数。 所以它看起来像这样:

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(); 
    } 
} 

但现在我得到异常: “死nicht-statische了Methode erfordert EIN ZIEL” 翻译是这样的: “非静态方法需要一个目标”

第一次递归调用函数时抛出异常。 属性是primaryMonitorSize,它的类型为Size。恕我直言,这与我解析类型Size而不是System.Windows.Forms.SystemInformation.primaryMonitorSize的事实有关,以便我知道实际的类型,但不知道我的程序的哪个成员,因为它也可能是winForm的大小。

那么我该如何解决这个问题?我感谢每一个建设性的批评。

@编辑:这是一个MSDN的例子。但它看起来不漂亮。 http://msdn.microsoft.com/de-de/library/system.windows.forms.systeminformation.powerstatus.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

回答

2

所有System.Windows.Forms.SystemInformation的属性都是静态的,这就是为什么你没有传递对象和

非静态方法需要一个目标

异常不发生在你的第一个代码示例中。对于不是静态的属性,当您调用GetValue(object obj, object[] index)方法时需要使用目标。

看看这个控制台应用程序的注意,在第二foreach,该GetValue方法,其实需要SystemInformation.PowerStatus(目标实例)作为第一个参数:

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)); 
     } 
    } 
} 
+0

谢谢你,但你的代码的另一个问题。什么是逻辑或在GetProperties完成。只分离静态和非静态属性。公众和非公众也有点混淆 –

+0

看看MS文章的方法:[Type.GetProperties方法(BindingFlags)](http://msdn.microsoft.com/en-us/library/kyaxdd3x。 ASPX)。 *备注*部分的解释非常好。 –

3
private void readProperties(Type T, int indent) 

您需要改进这种方法,它不适合读取PowerStatus属性的属性。这需要一个对象,你不能将null传递给GetValue()方法。所以写像这样代替:

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); 
    } 
} 

注意,现在如何读取属性值,如果它是一个对象,然后将其再次通过该对象readProperties这样的GetValue()将正常工作。将null传递给SystemInformation。

+0

感谢您解决问题。 –