Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过表达式模拟字符串+字符串?_C#_Expression - Fatal编程技术网

C# 如何通过表达式模拟字符串+字符串?

C# 如何通过表达式模拟字符串+字符串?,c#,expression,C#,Expression,如何通过c表达式模拟字符串+字符串表达式。Expression.Add方法无效 字符串+字符串表达式 111+222=111222 谢谢您需要调用到string.Concat中,C编译器将字符串连接转换为对string.Concat的调用 var concatMethod = typeof(string).GetMethod("Concat", new[] { typeof(string), typeof(string) }); var first = Expression.Const

如何通过c表达式模拟字符串+字符串表达式。Expression.Add方法无效

字符串+字符串表达式

111+222=111222

谢谢

您需要调用到string.Concat中,C编译器将字符串连接转换为对string.Concat的调用

var concatMethod = typeof(string).GetMethod("Concat", new[] { typeof(string), typeof(string) });    

var first = Expression.Constant("a");
var second = Expression.Constant("b");
var concat = Expression.Call(concatMethod, first, second);
var lambda = Expression.Lambda<Func<string>>(concat).Compile();
Console.WriteLine(lambda()); // "ab"
事实上,如果你写

Expression<Func<string, string string>> x = (a, b) => a + b;
并在调试器中检查它,您将看到它生成了一个二进制表达式,方法为string.Concatstring,string,而不是MethodCallExpression。因此编译器实际上使用@kalimag的答案,而不是我的。但是,这两种方法都可以工作。

您需要调用到string.Concat中。C编译器将字符串连接转换为对string.Concat的调用

var concatMethod = typeof(string).GetMethod("Concat", new[] { typeof(string), typeof(string) });    

var first = Expression.Constant("a");
var second = Expression.Constant("b");
var concat = Expression.Call(concatMethod, first, second);
var lambda = Expression.Lambda<Func<string>>(concat).Compile();
Console.WriteLine(lambda()); // "ab"
事实上,如果你写

Expression<Func<string, string string>> x = (a, b) => a + b;
并在调试器中检查它,您将看到它生成了一个二进制表达式,方法为string.Concatstring,string,而不是MethodCallExpression。因此编译器实际上使用@kalimag的答案,而不是我的。但是,这两种方法都可以使用。

Expression.Add有一个重载,该重载采用MethodInfo,可以是与给定参数类型兼容的任何静态方法:

var concatMethod = typeof(string).GetMethod(nameof(String.Concat), new [] { typeof(string), typeof(string)});
var expr = Expression.Add(Expression.Constant("a"), Expression.Constant("b"), concatMethod);
实际上,这类似于Expression.Call,但它会生成不同的表达式树,并在调试器中以不同的方式显示。

Expression.Add有一个重载,该重载采用MethodInfo,该重载可以是与给定参数类型兼容的任何静态方法:

var concatMethod = typeof(string).GetMethod(nameof(String.Concat), new [] { typeof(string), typeof(string)});
var expr = Expression.Add(Expression.Constant("a"), Expression.Constant("b"), concatMethod);

实际上,这类似于Expression.Call,但它会生成不同的表达式树,并在调试器中以不同的方式显示。

仅Console.WriteLine000+111;林帕德是你的朋友。如果你反编译像var a=1111这样的东西;var b=222;var c=a+b;,您将看到这最终是对String.Concat的调用。@jeroemostert有没有办法使用LinqPad生成类似于canton7 answer的代码?我只能找到一个code@AleksAndreevSharpLab擅长显示下划线C,但在本例中,它将显示+。你需要到IL去看看发生了什么你可以看到call string[mscorlib]系统string::Concatstring,string:@AleksAndreev:我不知道。LINQPad可以从uIn new[]{}.AsQueryable中转储表达式树,让a=111让b=222选择a+b.expression,但这将打印实际的表达式,实际上是.Add,因此没有好处。再说一遍,我不介意读IL…只是控制台;林帕德是你的朋友。如果你反编译像var a=1111这样的东西;var b=222;var c=a+b;,您将看到这最终是对String.Concat的调用。@jeroemostert有没有办法使用LinqPad生成类似于canton7 answer的代码?我只能找到一个code@AleksAndreevSharpLab擅长显示下划线C,但在本例中,它将显示+。你需要到IL去看看发生了什么你可以看到call string[mscorlib]系统string::Concatstring,string:@AleksAndreev:我不知道。LINQPad可以从uIn new[]{}.AsQueryable中转储表达式树,让a=111让b=222选择a+b.expression,但这将打印实际的表达式,实际上是.Add,因此没有好处。再说一次,我不介意读IL。。。