0

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=..........
user2155362
  • 1,657
  • 5
  • 18
  • 30
  • 3
    Yes. However we need more details on what you're trying to do - having created the `Func`, you won't be able to reference it directly in your code: it will either have to be a `Delegate` or a `Func` or similar. – canton7 Mar 01 '19 at 12:46
  • 2
    Have a look at expression-trees for example. What do you need the func for? How do you want to use it? – MakePeaceGreatAgain Mar 01 '19 at 12:47
  • `Delegate.CreateDelegate` is also able to easily do this. However we can't give you exact code based on your question alone. – canton7 Mar 01 '19 at 12:48
  • 1
    Did you check this? https://stackoverflow.com/questions/17738499/create-dynamic-funct-tresult-from-object – Fatikhan Gasimov Mar 01 '19 at 12:48
  • Possible duplicate of [Create Dynamic Func from Object](https://stackoverflow.com/questions/17738499/create-dynamic-funct-tresult-from-object) – Alessandro D'Andria Mar 01 '19 at 12:49
  • This sounds like you need to put the method inside the class instead of overcomplicating thing. Or if you don't have access to it you are looking at extensions – Franck Mar 01 '19 at 12:54
  • The duplicate **might** solve OPs problem, however chances are high OP is **actually** interested in something different and has some very specific problem - which we don´t know. – MakePeaceGreatAgain Mar 01 '19 at 13:00
  • Generics should help here with building the expression tree – Nkosi Mar 01 '19 at 13:17
  • As you know your types only at runtime you can´t create a statically typed func, which would need the **compiler** to infer that type. So just use the [non-generic `Expression.Lambda`](https://learn.microsoft.com/dotnet/api/system.linq.expressions.expression.lambda?view=netframework-4.7.2#System_Linq_Expressions_Expression_Lambda_System_Type_System_Linq_Expressions_Expression_System_String_System_Boolean_System_Collections_Generic_IEnumerable_System_Linq_Expressions_ParameterExpression__) instead – MakePeaceGreatAgain Mar 01 '19 at 13:41

0 Answers0