C#,create(method?)表达式并调用它 [英] C#, create (method?) expression and call it

查看:146
本文介绍了C#,create(method?)表达式并调用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是c#表达式的新手。我有一些类似的东西

  class SimpleClass 
{
private string ReturnString(string InputString)
{
returnresult is:+ InputString;
}

public string返回(表达式exp)
{
LambdaExpression lambda = Expression.Lambda(exp);
return lambda.Compile();
}
}

现在,我想调用这个方法Return with一些paramter的东西(伪)这样:

  SimpleClass sc = new SimpleClass(); 
表达式表达式= Expression.MethodCall(//如何使用一些参数创建表达式来调用SimpleClass.ReturnString);
string result = sc.Return(expression);
Console.WriteLine(result);

感谢您的帮助/回答。



Matt

解决方案

尽早执行 exp 签名尽可能 - 即作为表达式< Func< string>>

  public string Return(Expression< Func< string>> expression)
{
return expression.Compile()();
}

与:

  SimpleClass sc = new SimpleClass(); 
string result = sc.Return(()=> sc.ReturnString(hello world));
Console.WriteLine(result);

或:

 code> SimpleClass sc = new SimpleClass(); 
表达式表达式= Expression.Call(
Expression.Constant(sc),// target-object
ReturnString,// method-name
null,// generic type -argments
Expression.Constant(hello world)//方法参数
);
var lambda = Expression.Lambda< Func< string>>(expression);
string result = sc.Return(lambda);
Console.WriteLine(result);

当然,委托使用( Func< string> )可能在许多情况下也可以正常工作。


I'm realtively new to c# expression. I have some class something like that

class SimpleClass
{
    private string ReturnString(string InputString)
    {
        return "result is: "+InputString;
    }

    public string Return(Expression exp)
    {
        LambdaExpression lambda = Expression.Lambda(exp);
        return lambda.Compile();
    }
}

Now, I would like to call this method Return with some paramter something (pseudo) like this:

      SimpleClass sc = new SimpleClass();
      Expression expression = Expression.MethodCall(//how to create expression to call SimpleClass.ReturnString with some parameter?);
     string result = sc.Return(expression);
    Console.WriteLine(result);

Thanks for help/answer.

Matt

解决方案

It would be better to enforce the exp signature as early as possible - i.e. as an Expression<Func<string>>

public string Return(Expression<Func<string>> expression)
{
    return expression.Compile()();
}

with either:

SimpleClass sc = new SimpleClass();
string result = sc.Return(() => sc.ReturnString("hello world"));
Console.WriteLine(result);

or:

SimpleClass sc = new SimpleClass();
Expression expression = Expression.Call(
    Expression.Constant(sc),           // target-object
    "ReturnString",                    // method-name
    null,                              // generic type-argments
    Expression.Constant("hello world") // method-arguments
);
var lambda = Expression.Lambda<Func<string>>(expression);
string result = sc.Return(lambda);
Console.WriteLine(result);

Of course, delegate usage (Func<string>) might work just as well in many scenarios.

这篇关于C#,create(method?)表达式并调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆