98

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);
}
Rush Frisby
  • 11,388
  • 19
  • 63
  • 83

5 Answers5

221

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

Alberto
  • 15,626
  • 9
  • 43
  • 56
  • 3
    Yep, this is somethings that's been required since `dynamic` was introduced. – khellang Apr 04 '14 at 14:06
  • 19
    And if Microsoft gave an error message which told you this is would make things so much easier. – kjbartel Sep 04 '15 at 01:30
  • 1
    I don't know if this fixed the issue or not, but I added into my Views/Web.config node. The compiler error is gone now. – Don Rolling Jan 12 '16 at 15:06
  • 3
    FWIW adding Microsoft.CSharp.dll means var scriptOptions = ScriptOptions.Default.WithReferences("Microsoft.CSharp") i.e. drop the dll. Stumped me for a few mins :) – Jon H Apr 06 '16 at 13:02
  • @JonH so we should add that line to AssemblyInfo.cs or somewhere **instead** of referencing the dll? – NH. Sep 22 '17 at 22:11
  • Exactly right. My issue was using this inside the Roslyn engine and what is referenced in the encompassing application isn't the same thing. – Jon H Sep 24 '17 at 07:16
  • I tried running the Rush code in a .net core 2.1 application console. I have added Microsoft.CSharp.dll reference in the project and also between the MetadataFileReference in the code, but I always get the same error. – Renzo Ciot Aug 21 '18 at 11:03
11

To make the code work in .Net Core 2.1 I had to add this references in the compilation:

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),            
    }
);
Renzo Ciot
  • 3,746
  • 2
  • 25
  • 29
  • You can get away with System.Linq.Expressions, System.Private.CoreLib, System.Runtime and Microsoft.CSharp, all as strings – Simon Mourier Aug 22 '18 at 13:15
  • 1
    To make my code work in .net 3.1 the key point was `MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("netstandard")).Location)` – Feri Apr 27 '21 at 07:04
7

ASP.NET MVC specific:

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

    [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 :-)

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • Figured it didn't apply to original question posted from 2014 (though wanted to acknowledge it was helpful.) Wasn't sure what SO etiquette dictated in such a situation. – t.j. Jan 03 '17 at 00:56
  • Fair point :) I just added it here because it seemed so obscure and this was a good match for that error – Simon_Weaver Jan 03 '17 at 00:58
4

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.

Community
  • 1
  • 1
A.Clymer
  • 472
  • 3
  • 9
3

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.

Kols
  • 3,641
  • 2
  • 34
  • 42
  • The Microsoft CSharp has to be referenced by your Roslyn compiland. It is available, or it can be added as a normal reference to your caller project. Remember for Roslyn use set Copy Local = true in the properties of the reference, else your generated code cannot find it. Then add MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.CSharp")).Location) to the references[] to add it to your compiland. – Goodies Nov 27 '20 at 13:38