2012-03-13 58 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

相关问题