vendredi 18 mai 2018

Calling a method of generic object without knowing its Type and passing a predicate parameter of this type to this method

Im using reflection and im looping through properties of my entity framework context and i want to call "Where" method on some of those properties. Those properties are DbSet.

var contextPropertyInfo = 
  context.GetType().GetProperties().Where(p=> p.Name.Contains("SewingCards")).ToList();

foreach (var info in contextPropertyInfo)
{
  var sewingCardDbSet = info.GetValue(context);
  var dbSetWhereMethod = sewingCardDbSet.GetType().GetMethod("Where");
  var genericTypes = sewingCardDbSet.GetType().GetGenericArguments().First();                      
}

Im getting the type of T by calling

var genericType = sewingCardDbSet.GetType().GetGenericArguments().First();

now i want to call this DbSet.Where method with

var dbSetWhereMethod = sewingCardDbSet.GetType().GetMethod("Where");
dbSetWhereMethod.Invoke(context, parameters);

Calling this method wont work until in parameters i will form a predicate that is based on "genericType" and i dont really know what to do next. How can i call this method with reflection?





Aucun commentaire:

Enregistrer un commentaire