0

I am trying to use Rosyln to dynamically compile some code into an assembly. I have it all working with one exception: when I try to declare a type as dynamic, I get a compiler error (from the runtime compile) saying:

error CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'

Some research—such as the Stack Overflow thread Using System.Dynamic in Roslyn—suggests including a reference to the Microsoft.CSharp.dll and to the dynamic attribute, which I did.

Again, if I switch the type in the code text to int, it works just fine. It requires the Microsoft.CodeAnalysis.Compilers NuGet package. If it matters, I am using a console application with .NET Core 2.2 on Windows.

For reference, this code is based on the useful Compiling and Running C# Code in Runtime.

Any suggestions?


using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;

namespace RoslynTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var assemblyName = "TestLibrary";
            var code = @"
    namespace TestNamespace
    {
        public class TestClass
        {
            public static int Add(dynamic a, int b)  // Works if a is declared as int
            {
                return a+b;
            }
        }
    }";

            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
            CSharpCompilation compilation = CSharpCompilation.Create(
                assemblyName,
                new[] { syntaxTree },
                new MetadataReference[]
                {
                    MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                    MetadataReference.CreateFromFile(typeof(DynamicAttribute).Assembly.Location),
                    MetadataReference.CreateFromFile(typeof(Microsoft.CSharp.RuntimeBinder.Binder).Assembly.Location)
                },
                new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using (var memoryStream = new MemoryStream())
            {
                EmitResult result = compilation.Emit(memoryStream);

                if (result.Success)
                {
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    Assembly assembly = Assembly.Load(memoryStream.ToArray());

                    Type testClassType = assembly.GetType("TestNamespace.TestClass");
                    var addResult = (int)testClassType.GetMethod("Add").Invoke(null, new object[] { 3, 4 });
                    Console.WriteLine($"Result is {addResult}");
                }
                else
                {
                    foreach(var error in result.Diagnostics)
                        Console.WriteLine(error.ToString());
                }

                Console.ReadLine();
            }
        }
    }
}
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Fraser Orr
  • 361
  • 1
  • 3
  • 19

0 Answers0