Get the parent class instance

I would like to ask for help. I have the following class:

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

...

 POCOConfiguration instance = new POCOConfiguration (); 

How can I get a POCO type from an instance ?

thanks

+6
source share
5 answers
 instance.GetType().BaseType.GetGenericArguments()[0] 
+9
source

Take a look at this: http://msdn.microsoft.com/en-us/library/b8ytshk6.aspx

There is an example showing how to get a type (steps 3,4 and 5)

0
source

If the class has a constructor without a parameter, you can write:

 public class POCOConfiguration : EntityTypeConfiguration<POCO> where POCO : new() { public POCOConfiguration() { var poco = new POCO(); } } 

Otherwise, you must use reflection, see the Activator class.

0
source

check this

 POCOConfiguration instance = new POCOConfiguration(); Type t = instance.GetType().BaseType.GetGenericArguments()[0]; //here t is the type of POCO 

will work...

0
source

If I understand correctly, you want to get a POCO type. Then declare the method and name it as follows:

 public Type GetTypeOfPoco<T>(EntityTypeConfiguration<T> entityTypeConfiguration) { Type t = typeof(T); return t; } 

name him;

 Type t = GetTypeOfPoco(instance); 
0
source

Source: https://habr.com/ru/post/916427/


All Articles