Can I build Func<> with dynamically type. What I mean is is there any way to build func like below:
Func<typeof(myclass),bool> f=..........
or
Type myType;
Func<myType,bool> f=............
In my project, I need to build expression base on the the class below:
class Variable{
public Type VarType{get;set;}
public string Name{get;set;}
public object Value{get;set;}
}
The method below build expression base on the value property, if value is null, I should build a lambdeexpression, if value is not null I should build a constantexpression
Expression ToExpression(Variable variable){
if(variable == null){
// return constantexpression
}else{
string[] strs = variable.Name.split(new char[1] { '.' }, StringSplitOptions.RemoveEmptyEntries);
ParameterExpression parameter = Expression.Parameter(variable.VarType, strs[0])
MemberExpression member = Expression.PropertyOrField(parameter, strs[1]);
ConstantExpression constant = Expression.Constant(someValue);
return Expression.Lambda<Func<variable.VarType, bool>>(Expression.GreaterThan(member, constant), parameter);
}
}
But now, it seem the Expression.Lambda<Func<variable.VarType, bool>>
is wrong.
So my question is there any way to build the func like
Func<typeof(myclass),bool> f=..........