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
'Programming' 카테고리의 다른 글
커밋의 파일 변경 사항 되돌리기 (0) | 2020.08.27 |
---|---|
Inset Box Shadow 한쪽에만? (0) | 2020.08.27 |
프로그래밍 방식으로 HTML SELECT에 드롭 다운을 지시하는 방법 (예 : 마우스 오버로 인해) (0) | 2020.08.27 |
유효한 국제 전화 번호와 일치하는 정규 표현식은 무엇입니까? (0) | 2020.08.27 |
Windows 경로 대신 DOS 경로 가져 오기 (0) | 2020.08.27 |