罗斯林使用System.Dynamic [英] Using System.Dynamic in Roslyn
问题描述
我修改自带的罗斯林的新版本是昨天发布使用动态和ExpandoObject,但我得到它,我不知道如何解决一个编译器错误的例子。该错误是:
(7,21):错误CS0656:缺少编译器要求会员'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create
块引用>
你能不能用在新的编译器的动态吗?我该如何解决这个问题?下面是我更新的例子:
[TestMethod的]
公共无效EndToEndCompileAndRun()
{
变种文字= @使用System.Dynamic;
公共类计算器
{
公共静态对象的评估()
{
动态X =新ExpandoObject( );
x.Result = 42;
返回x.Result;
}
};
VAR树= SyntaxFactory.ParseSyntaxTree(文本);
VAR编译= CSharpCompilation.Create(
calc.dll,
选项:新CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
syntaxTrees:新[] {}树,
参考:新的[] {新MetadataFileReference(typeof运算(对象).Assembly.Location),新MetadataFileReference(typeof运算(ExpandoObject).Assembly.Location)});
大会compiledAssembly;
使用(VAR流=新的MemoryStream())
{
VAR compileResult = compilation.Emit(流);
compiledAssembly =的Assembly.Load(stream.GetBuffer());
}
型计算器= compiledAssembly.GetType(计算器);
MethodInfo的评估= calculator.GetMethod(评估);
串答案= evaluate.Invoke(NULL,NULL)的ToString();
Assert.AreEqual(42,答案);
}
解决方案我认为你应该引用在
Microsoft.CSharp.dll
组装I modified the example that comes with the new version of Roslyn that was released yesterday to use dynamic and ExpandoObject but I am getting a compiler error which I am not sure how to fix. The error is:
(7,21): error CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'
Can you not use dynamics in the new compiler yet? How can I fix this? Here is the example that I updated:
[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); }
解决方案I think that you should reference the
Microsoft.CSharp.dll
assembly这篇关于罗斯林使用System.Dynamic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!