Programming

Roslyn에서 System.Dynamic 사용

procodes 2020. 8. 27. 22:29
반응형

Roslyn에서 System.Dynamic 사용


어제 출시 된 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);
}

Microsoft.CSharp.dll어셈블리를 참조해야한다고 생각합니다


.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),            
    }
);

ASP.NET MVC 관련 :

당신이 넣어 잊어 버린 경우 당신은 MVC 6 컨트롤러에서이 오류를 얻을 수 있습니다 [FromBody]A의 POST방법.

    [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.

With [FromBody] added you can now post JSON and then just dynamically access the properties :-)


You may also want to check the properties of all your project references. Make sure any reference is using .NET newer than 2.0. 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.

See this post.

Also, don't forget to add the Microsoft.CSharp reference to you main project like @Alberto said.


If your project is targeting .Net Core or .Net Standard, then instead of adding reference you can install the Microsoft.CSharp NuGet package to solve this error.

참고URL : https://stackoverflow.com/questions/22864790/using-system-dynamic-in-roslyn

반응형