dimanche 18 juin 2017

Cast of generic property through reflection

I'm trying to retrieving a DbContext DbSet property by its name but I don't know how to handle the generic parameter.

DataSource.Load(IQuerable source) method comes from an external dll and I cannot modify it.

Knowing the property name of my DbSet prop (from an entity framework dbcontext class) I want to use that property value as parameter for DataSource.Load

 public class DataManager
 {

    private MyDbContext _dbContext;

    public DataManager(MyDbContext dbContext)
    {
        _dbContext = dbContext;
    }


    public object Load(string propName)
    {
        var source = _dbContext.GetType().GetProperty(entityName).GetValue(_dbContext, null);

        return DataSourceLoader.Load(source);         
    }

    //DataSourceLoader.Load signature:
    //DataSourceLoader.Load<T>(System.Linq.IQueryable<T>)





Aucun commentaire:

Enregistrer un commentaire