通过表达式树读取行的最佳方法是什么?
如果我想将用户输入从控制台获取到我的表达式树。 最好的方法是什么? 以及如何使变量“名称”鸭子输入?
这是我的代码。
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();
}
}
}
If I want to get a user input from Console to my Expression Tree. What is the best way to do it? and how to make variable 'name' duck typing?
Here are my code.
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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
表达式树被设计为执行固定操作 - 特别是,成员访问在创建表达式树时需要一个已知的
MemberInfo
(等)(因为它们是不可变的)。您可以复制从
dynamic
生成的代码< /a> 如果您正在使用 4.0,但说实话,在这种情况下更好的方法很简单:不要使用表达式树。对于这种对成员的动态访问,反射或 ComponentModel (
TypeDescriptor
) 都是理想的选择。另外 - 对仅使用一次的内容调用
Compile
不会节省任何时间,使用DynamicInvoke
也不会节省时间......您需要使用类型化委托表单(调用
)。Expression trees are designed to perform a fixed operation - in particular, the member-access is going to want a known
MemberInfo
(etc) at the point of expression tree creation (since they are immutable).You could duplicate the generated code from
dynamic
if you are playing with 4.0, but to be honest, the better approach in this scenario is simply: don't use an expression tree.Either reflection or
ComponentModel
(TypeDescriptor
) would be ideal for this dynamic access to a member.Also - calling
Compile
on something you use only once isn't saving any time, and usingDynamicInvoke
isn't either... you need to use the typed delegate form (Invoke
).