Question: I have three class as following:

```
public class TestEntity { }

public class BaseClass<TEntity> { }

public class DerivedClass : BaseClass<TestEntity> { }

```

I already get the `System.Type` object of `DerivedClass` using reflection in runtime. How can I get the `System.Type` object of `TestEntity` using reflection?

Thanks.

Answer: 
You can use the BaseType property. The following code would be resilient for changes in the inheritance (e.g. if you add another class in the middle):

```
Type GetBaseType(Type type)
{
   while (type.BaseType != null)
   {
      type = type.BaseType;
      if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(BaseClass<>))
      {
          return type.GetGenericArguments()[0];
      }
   }
   throw new InvalidOperationException("Base type was not found");
}

// to use:
GetBaseType(typeof(DerivedClass))

```
```
var derivedType = typeof(DerivedClass);
var baseClass = derivedType.BaseType;
var genericType = baseClass.GetGenericArguments()[0];

```