콘솔에서 내 Expression Tree로 사용자 입력을 받고 싶습니다. 가장 좋은 방법은 무엇입니까? 변수 '이름'을 입력하는 방법은 무엇입니까?Expression Tree를 사용하여 ReadLine을 만드는 가장 좋은 방법은 무엇입니까?
내 코드는 다음과 같습니다.
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();
}
}
}
@MarkGravell : 표현식 트리와 동적 유형에 대한 귀하의 답을 찾았으며 매우 도움이되었습니다. 고맙습니다! 그러나 나는 [이 문제] (http://stackoverflow.com/q/24939618/938668)에서 곤란을 겪고있다. 당신이 볼 기회를 얻으면, 나는 그것에 대한 당신의 생각에 감사 할 것입니다. –