简体   繁体   中英

How to get dynamically the properties from a object T from a dynamically objectset<t> using entity framework?

I'm trying to create a report screen that adjust itself dynamically to the contents of a database view that is present in a model generated with entity framework.

To get the entity i'm using this code:

var view = context.GetType().GetProperty(viewName).GetValue(context, null);

This code returns the ObjectSet<viewType> that fulfill part of my needs.

Now I need to get the properties of the view type so I can get the columns.

I tryed to used something like that:

var methodInfo = view .GetType().GetMethod("First");
var properties = methodInfo.ReturnType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

But its not finding the method "First" and I don't know exactly where to find some help of how can I make this works.

You're trying to get a method first() of ObjectSet<viewType> when you really want viewType , I believe. So this is what you need.

var methodInfo = view.GetType().GetGenerericArguments()[0].GetMethod("First");

首先是System.Linq.Queryable的扩展方法。

var gtype = view.GetType().GetGenericArguments()[0];

If view is ObjectSet<FooBars>

gtype will be FooBars

Then you can call GetProperties on gtype.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM