Convert Method Group to Expression

后端 未结 3 1803
情深已故
情深已故 2021-01-17 16:00

I\'m trying to figure out of if there is a simple syntax for converting a Method Group to an expression. It seems easy enough with lambdas, but it doesn\'t translate to meth

相关标签:
3条回答
  • 2021-01-17 16:30

    It is also possible to do it using NJection.LambdaConverter a Delegate to LambdaExpression converter Library

    public class Program
    {
        private static void Main(string[] args) {
           var lambda = Lambda.TransformMethodTo<Func<string, int>>()
                              .From(() => Parse)
                              .ToLambda();            
        }   
    
        public static int Parse(string value) {
           return int.Parse(value)
        } 
    }
    
    0 讨论(0)
  • 2021-01-17 16:41

    I use property instead of method.

    public class MathLibrary
    {
        public Expression<Func<int, int>> AddOne {  
            get {   return input => input + 1;} 
        }
    }
    

    Using above

    0 讨论(0)
  • 2021-01-17 16:42

    How about this?

      Expression<Func<int, int>> funcExpr2 = (pArg) => foo.AFuncIntInt(pArg);
      Expression<FuncIntInt> delExpr2 = (pArg) => foo.AFuncIntInt(pArg);
    
    0 讨论(0)
提交回复
热议问题