我修改了昨天发布的新版本Roslyn附带的示例,以使用动态和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);
}
dynamic
引入以来,这是必需的。