How to get column type by name using reflation?

I am using the framework 6 entity in my first project code project. a context variable class that inherits from the DbContext type.

I have this code:

   var table = (IEnumerable)context.GetType().
               GetProperty("TableName").
               GetValue(context, null);

From tableabove, I need to get the column type by its name.

Any idea how I can implement it?

+4
source share
1 answer

Instead, IEnumerableyou need to attribute the table to IQueryableand use the IQueryable.ElementType property to get the required information, like this

var table = (IQueryable)context.GetType().
               GetProperty("TableName").
               GetValue(context, null);
var propertyType = table.ElementType.GetProperty("PropertyName").PropertyType;
+3
source

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


All Articles