2012-03-13 39 views
0

我正在使用Reflection來獲取我在C#中的類的所有字段,但現在我想要在我的類中獲取每個變量的GC生成。我怎樣才能做到這一點?如何使用字符串作爲對象名稱來調用GC.GetGeneration()?

CSkyclass 
{ 
    float time = 0; 
} 


Sky = new CSkyclass(); 


void GetGeneration() 
{ 
    FieldInfo[] FieldArray = typeof(CSkyclass).GetFields(flags); 

    foreach(System.Reflection.FieldInfo Field in FieldArray) 
    { 
     string name = Field.Name; //"time" 
     int g = GC.GetGeneration(name); //should = GC.GetGeneration(Sky.time); 

    } 

} 

這可能嗎? 感謝

回答

1

你正在試圖獲得該字段的值產生:

GC.GetGeneration(field.GetValue(someInstance)); 
+0

謝謝,這做到了。 – rwg 2012-03-14 01:04:18

相關問題