c# - How to get TEntity by name -


i need tentitys mi dbcontext extract information each table. have little tables little information. have code don´t work.

var dbsetpropertieslocal = local.getdbsetproperties();                  foreach (var item in dbsetpropertieslocal) {     type type = item.propertytype; // need type of actual tentity     var enumerable = getlocal<type>(item.name, local);   }                  

this method returns data specified dbset name , te specified dbcontext

public list<tentity> getlocal<tentity>(string name, dbcontext ctx) {     var enumerable = (ienumerable<tentity>)(typeof([classnameofmycontext]).getproperty(name).getvalue(ctx, null));     return enumerable.tolist(); } 

this method properties dbcontext. use dbsets in dbcontext

public static list<propertyinfo> getdbsetproperties(this dbcontext context) {     var dbsetproperties = new list<propertyinfo>();     var properties = context.gettype().getproperties();      foreach (var property in properties)     {         var settype = property.propertytype;         var isdbset = settype.isgenerictype && (typeof(idbset<>).isassignablefrom(settype.getgenerictypedefinition()) || settype.getinterface(typeof(idbset<>).fullname) != null);          if (isdbset)             dbsetproperties.add(property);     }      return dbsetproperties; } 

i'm not sure if current path best way solve bigger problem (implementing database synchronizer). here solution smaller problem (calling generic code).

instead of extension method, create generic class, , create instance using reflection. also, add interface talk instance. remember, class might generic, if have talk non-generic code, need interface convert generic class instance non-generic interface instance (if know mean).

public interface ilocalentitylistprovider {     list<object> getlocal(string name, dbcontext ctx); } public class localentitylistprovider<tentity> : ilocalentitylistprovider {     private ienumerable<tentity> getlocal(string name, dbcontext ctx)     {         return (ienumerable<tentity>)(typeof([classnameofmycontext])             .getproperty(name)             .getvalue(ctx, null));     }      list<object> ilocalentitylistprovider.getlocal(string name, dbcontext ctx) {         return getlocal(name, ctx)             .cast<object>()             .tolist();     } }  var dbsetpropertieslocal = local.getdbsetproperties();                 foreach (var item in dbsetpropertieslocal) {     var entitytype = item.propertytype.generictypearguments.first();     var providerclasstype = typeof(localentitylistprovider<>).makegeneric(entitytype);     var providerinstance = activator.createinstance(providerclasstype) ilocalentitylistprovider;     var enumerable = providerinstance.getlocal(item.name, local);   } 

note: haven't tested this, have used principle many times.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -