lundi 4 avril 2016

How to get fields of a generic type in C#

Say I have a generic class

class Foo<T>
{
   T fooVal;
   public T FooVal { get { return fooVal; } }
}

and I want to get the FieldInfo for fooVal from an instantiated type:

Foo<int> fooInt = new foo<int>();
FieldInfo fooValField = fooInt.GetType().GetField("fooVal");

The problem is, fooValField is then null. Even if I call GetFields() it returns an empty array. I know the Type is correct because reflection tells me it is Foo'1. I just can't figure out why it doesn't see the fooVal field. Is there something I'm missing here? I can see the FooVal property if I call GetProperties, so I would expect the fooVal field to also show up?





Aucun commentaire:

Enregistrer un commentaire