Reputation: 34180
I have classes like this:
class A
{
public int x;
public int y;
}
class B
{
public int x;
public int y;
}
class C
{
public A a;
public B b;
}
public class Test<T> where T : class, new()
{
public Test()
{
FieldInfo[] x = typeof(T).GetFields().ToArray();
Type y = x[0].GetType();
}
}
When I declare Test<C> c = new T
typeof(T).GetFields().ToArray()
Returns the two fields a of type A
and b of type B
as expected. but when I want to get type of first field itself, like this:
Type y = x[0].GetType();
, the value of y is System.Reflection.RtFieldInfo
and not A
Why is it so? I need to get to the class A
so that I can get its fields and ...
Upvotes: 1
Views: 8470
Reputation: 164341
You can use FieldType
on the returned FieldInfo
for that.
GetType()
returns the type of current object, which is FieldInfo
, and not the type that it describes.
FieldInfo
is a class that describes a field and it's type; and the type information for the field it describes is stored in FieldInfo
.
Upvotes: 4