2013-06-12 22 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

感謝您解決問題。 –