我修改了昨天发布的新版本Roslyn附带的示例,使用了dynamic和ExpandoObject,但是我遇到了一个编译错误,不知道该如何解决。错误信息如下:
(7,21):错误 CS0656:缺少编译器所需的成员 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'
在新编译器中不能使用动态类型吗?我该如何解决这个问题?这是我更新后的示例:
[TestMethod]
public void EndToEndCompileAndRun()
{
var text = @"using System.Dynamic;
public class Calculator
{
public static object Evaluate()
{
dynamic x = new ExpandoObject();
x.Result = 42;
return x.Result;
}
}";
var tree = SyntaxFactory.ParseSyntaxTree(text);
var compilation = CSharpCompilation.Create(
"calc.dll",
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
syntaxTrees: new[] {tree},
references: new[] {new MetadataFileReference(typeof (object).Assembly.Location), new MetadataFileReference(typeof (ExpandoObject).Assembly.Location)});
Assembly compiledAssembly;
using (var stream = new MemoryStream())
{
var compileResult = compilation.Emit(stream);
compiledAssembly = Assembly.Load(stream.GetBuffer());
}
Type calculator = compiledAssembly.GetType("Calculator");
MethodInfo evaluate = calculator.GetMethod("Evaluate");
string answer = evaluate.Invoke(null, null).ToString();
Assert.AreEqual("42", answer);
}