Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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#_.net_Lambda_Expression Trees - Fatal编程技术网

C# 通过表达式树读取行的最佳方式是什么?

C# 通过表达式树读取行的最佳方式是什么?,c#,.net,lambda,expression-trees,C#,.net,Lambda,Expression Trees,如果我想从控制台获取用户输入到表达式树。最好的方法是什么?以及如何使变量“name”duck类型化 这是我的密码 using System; using System.Reflection; using System.Collections.Generic; using Microsoft.Linq; using Microsoft.Linq.Expressions; namespace ExpressionTree { class Program { stat

如果我想从控制台获取用户输入到表达式树。最好的方法是什么?以及如何使变量“name”duck类型化

这是我的密码

using System;
using System.Reflection;
using System.Collections.Generic;
using Microsoft.Linq;
using Microsoft.Linq.Expressions;

namespace ExpressionTree
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Expression> statements = new List<Expression>();

            // Output
            MethodInfo Write = typeof(System.Console).GetMethod("Write", new Type[] { typeof(string) });
            ConstantExpression param = Expression.Constant("What is your name? ", typeof(string));
            Expression output = Expression.Call(null, Write, param);
            statements.Add(output);

            // Input
            MethodInfo ReadLine = typeof(System.Console).GetMethod("ReadLine");
            ParameterExpression exprName = Expression.Variable(typeof(String), "name");
            Expression exprReadLine = Expression.Call(null, ReadLine);

            // .NET 4.0 (DlR 0.9) from Microsoft.Scripting.Core.dll
            // Expression.Assign and Expression.Scope
            ScopeExpression input = Expression.Scope(Expression.Assign(exprName, exprReadLine), exprName);
            statements.Add(input);

            // Create the lambda
            LambdaExpression lambda = Expression.Lambda(Expression.Block(statements));

            // Compile and execute the lambda
            lambda.Compile().DynamicInvoke();

            Console.ReadLine();
        }
    }
}
使用系统;
运用系统反思;
使用System.Collections.Generic;
使用Microsoft.Linq;
使用Microsoft.Linq.Expressions;
名称空间表达式树
{
班级计划
{
静态void Main(字符串[]参数)
{
List语句=新列表();
//输出
MethodInfo Write=typeof(System.Console).GetMethod(“Write”,新类型[]{typeof(string)});
ConstantExpression param=Expression.Constant(“您的名字是什么?”,typeof(string));
表达式输出=Expression.Call(null、Write、param);
添加(输出);
//输入
MethodInfo ReadLine=typeof(System.Console).GetMethod(“ReadLine”);
ParameterExpression exprName=Expression.Variable(typeof(String),“name”);
Expression exprReadLine=Expression.Call(null,ReadLine);
//Microsoft.Scripting.Core.dll中的.NET 4.0(DlR 0.9)
//Expression.Assign和Expression.Scope
ScopeExpression输入=Expression.Scope(Expression.Assign(exprName,exprReadLine),exprName);
语句。添加(输入);
//创建lambda
LambdaExpression lambda=Expression.lambda(Expression.Block(语句));
//编译并执行lambda
lambda.Compile().DynamicInvoke();
Console.ReadLine();
}
}
}

表达式树设计用于执行固定操作-尤其是,成员访问在表达式树创建时需要一个已知的
MemberInfo
(etc)(因为它们是不可变的)

如果您使用的是4.0,您可以复制,但是老实说,在这个场景中更好的方法就是:不要使用表达式树

反射或
ComponentModel
TypeDescriptor
)都是这种对成员的动态访问的理想选择


另外-对只使用一次的东西调用
Compile
不会节省任何时间,而使用
DynamicInvoke
也不会。。。您需要使用类型化委托表单(
Invoke
)。

表达式树被设计为执行固定操作-尤其是,成员访问在表达式树创建时需要一个已知的
MemberInfo
(etc)(因为它们是不可变的)

如果您使用的是4.0,您可以复制,但是老实说,在这个场景中更好的方法就是:不要使用表达式树

反射或
ComponentModel
TypeDescriptor
)都是这种对成员的动态访问的理想选择


另外-对只使用一次的东西调用
Compile
不会节省任何时间,而使用
DynamicInvoke
也不会。。。您需要使用类型化委托表单(
Invoke
)。

@markgravel:我发现您关于表达式树和动态类型的答案非常有用。非常感谢。然而,我被难住了。如果你有机会看一看,我会很感激你的想法。@MarkGravell:我发现你关于表达式树和动态类型的答案非常有用。非常感谢。然而,我被难住了。如果你有机会看一看,我会很感激你的想法。