EDIT
My problem is this.
I need to generate a lambda expression that is of a type that I will only know at the time of execution, ie the type to be used will be passed in the classType parameter of the controller below. But the type I create in the runtime method is not compatible with the expression I need to create.
NOTE: The WhereEqualsNonTyped method recognizes the generated context type, but when using typeof () it returns the type 'System.object'.
Below the controller that calls the method to get the class type dynamically:
[Route ("{classtype} / {id} / {key}")]
public string GetObter (int id, string key, string classtype)
{
var attach = servico.ObterDinamico (classtype, id, includes);
return attach.RetrieveFiles (key);
}
}
Method that creates the context from the past type:
public object ObterDinamico(string typeName, int id, string [] includes = null)
{
Type tableEntity = Type.GetType ("My_Domain." + TypeName + ", My_Domain");
IEnumerable <dynamic> dbObject = (IEnumerable <dynamic>)
typeof (DbContext) .GetMethod ("Set", Type.EmptyTypes)
.MakeGenericMethod (tableEntity)
.Invoke (bd, null);
IQueryable <dynamic> result = dbObject.AsQueryable ();
if (includes! = null)
foreach (string i in includes)
result = result.Include (i);
return result.WhereEqualsNonTyped ("id", id, typeName);
}
Lambda method that does not recognize type T in this case, so it does not generate the expression as accurate:
public static IQueryable <T> WhereEqualsNonTyped <T> (this IQueryable <T> query, string propertyName, dynamic value, string typeName)
{
try
{
//sem essa instancia do Type para gerar o tipo dinâmico que preciso
//o método sempre dá erro assim que chega na linha
//MemberExpression
var type = Type.GetType ("My_Domain." + typeName + ", My_Domain");
ParameterExpression parameter = Expression.Parameter (type, "type");
MemberExpression property = Expression.Property (parameter, propertyName);
BinaryExpression expression = Expression.Equal (property, Expression.Constant (value, value.GetType ()));
Expression <Func <T, bool >> predicate = Expression.Lambda <Func <T, bool >> (expression, parameter);
return query.Where (predicate);
}
catch (Exception e)
{
return query;
}
}
Currently the error that is generated is:
ParameterExpression of type 'My_Domain.my_object' can not be used to delegate the parameter of type 'System.Object'