Using System.Dynamic in Roslyn

I changed the example that comes with the new version of Roslyn that was released yesterday to use dynamic and ExpandoObject, but I get a compiler error that I don’t know how to fix. Mistake:

(7,21): error CS0656: The required compiler member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create' is missing

Can you use dynamics in the new compiler? How can i fix this? Here is an 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); } 
+63
c # roslyn
Apr 04 '14 at 13:53 on
source share
3 answers

I think you should reference the Microsoft.CSharp.dll assembly

+188
Apr 04 '14 at 2:04
source share

You can get this error in MVC 6 if you forget to put [FromBody] in the POST method.

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

The default .NETCore project already contains a Microsoft.CSharp link, but you get almost the same message.

By adding [FromBody] you can post JSON and then just dynamically access the properties [FromBody]

+3
Oct 30 '16 at 3:00
source share

You can also check the properties of all project links. Make sure that any link uses .NET later than 2.0. I have a project that referenced another project in the same solution and had to rebuild the dependency using the newer purpose of the .NET platform.

See this post .

Also, be sure to add the Microsoft.CSharp link to the main project, such as @Alberto.

+2
Feb 26 '16 at 19:57
source share



All Articles