简体   繁体   English

在Roslyn中使用System.Dynamic

[英]Using System.Dynamic in Roslyn

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. 我修改了昨天发布的新版Roslyn附带的示例,以使用动态和ExpandoObject,但我收到编译器错误,我不知道如何修复。 The error is: 错误是:

(7,21): error CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create' (7,21):错误CS0656:缺少编译器所需的成员'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);
}

我认为你应该引用Microsoft.CSharp.dll程序集

ASP.NET MVC specific: ASP.NET MVC具体:

You can get this error in an MVC 6 controller if you forget to put [FromBody] in a POST method. 如果忘记将[FromBody]放在POST方法中,则可以在MVC 6控制器中出现此错误。

    [HttpPost("[action]")]
    public void RunReport([FromBody]dynamic report)
    {
        ...
    }

The .NETCore default project already includes Microsoft.CSharp reference but you get pretty much the same message. .NETCore默认项目已包含Microsoft.CSharp引用,但您得到的信息几乎相同。

With [FromBody] added you can now post JSON and then just dynamically access the properties :-) 添加[FromBody]您现在可以发布JSON,然后只需动态访问属性:-)

To make the code work in .Net Core 2.1 I had to add this references in the compilation: 为了使代码在.Net Core 2.1中工作,我必须在编译中添加这些引用:

var compilation = CSharpCompilation.Create(
    "calc.dll",
    options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
    syntaxTrees: new[] {tree},
    references: new[] {
        MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
        MetadataReference.CreateFromFile(typeof(ExpandoObject).Assembly.Location),
        MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.CSharp")).Location),
        MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("netstandard")).Location),
        MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("mscorlib")).Location),
        MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Runtime")).Location),            
    }
);

You may also want to check the properties of all your project references. 您可能还想检查所有项目引用的属性。 Make sure any reference is using .NET newer than 2.0. 确保任何引用都使用比2.0更新的.NET。 I have a project that was referencing another project in the same solution and had to rebuild the dependency using a newer .NET framework target. 我有一个项目在同一个解决方案中引用另一个项目,并且不得不使用较新的.NET框架目标重建依赖项。

See this post . 看这篇文章

Also, don't forget to add the Microsoft.CSharp reference to you main project like @Alberto said. 另外,不要忘记将Microsoft.CSharp引用添加到主项目中,如@Alberto所说。

如果您的项目以.Net Core或.Net Standard为目标,那么您可以安装Microsoft.CSharp NuGet包来解决此错误,而不是添加引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM