如何使用字符串作为对象名称调用GC.GetGeneration()?

时间:2012-03-13 17:18:57

标签: garbage-collection system.reflection

我正在使用Reflection来获取c#中所有类的字段,但现在我想在我的类中获取每个变量的GC生成。我怎么能这样做?

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 个答案:

答案 0 :(得分:1)

您正在尝试生成字段的

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