mercredi 7 mars 2018

Chained methods reflection execution

I need to execute some chained methods using reflection.

What I´m trying to get is an IQueryable of a Entity Core DBContext Set of a dynamic type:

this.DBContext.Set<TDynamicType>().AsQueryable();

Obviously, that code does not compiles because the type most be defined.

To solve that, I tried to execute the methods using reflection:

    MethodInfo dbSetMethod = typeof(DbContext).GetMethod(nameof(DbContext.Set));
    MethodInfo generic = dbSetMethod.MakeGenericMethod(property.DeclaringType);

    var asQueryableMethod = generic.ReturnType.GetMethod("AsQueryable");
    var result = asQueryableMethod.Invoke(this.DbContext, null);

But when I debug the code, I get null in the line:

var asQueryableMethod = generic.ReturnType.GetMethod("AsQueryable");

Apparently, the dbContext.Set() does not has the AsQueryable method. That method is an extension method coming from Linq I guess.

What I´m missing? Why is the method unavailable?





Aucun commentaire:

Enregistrer un commentaire