Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get parent of class of instance

Tags:

c#

I have the following class:

public class POCOConfiguration : EntityTypeConfiguration<POCO>
{
    public POCOConfiguration()
    {

    }
}
POCOConfiguration instance = new POCOConfiguration();

How can I get the type POCO from the instance?

Thanks

like image 895
Vojtech B Avatar asked May 23 '12 07:05

Vojtech B


2 Answers

instance.GetType().BaseType.GetGenericArguments()[0]
like image 103
Ian Newson Avatar answered Oct 28 '22 11:10

Ian Newson


Another answer is simply that instance.GetType().BaseType returns the base Type of class o parent class.

instance.GetType().BaseType.GetGenericArguments()[0] can throw an exception.

like image 29
J.Oliver Avatar answered Oct 28 '22 10:10

J.Oliver