6

I'm dynamically building a class that uses Microsoft.OData.Client this library have references to both mscorlib 2.0.5.0 (i guess the PCL) and 4.0.0.0. enter image description here

I want to compile my class with roslyn, as part of a larger program, but I can't seem to get it to work. My Roslyn compiler code is pretty minimalistic

SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(s);
            string assemblyName = Path.GetRandomFileName();

            List<MetadataReference> references = new List<MetadataReference>()
            {
                MetadataReference.CreateFromFile(typeof(DataServiceActionQuery).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(ODataAction).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(GeneratedCodeAttribute).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(IEdmModel).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(TimeOfDay).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(XmlDocument).Assembly.Location),
               // MetadataReference.CreateFromFile(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.0\Profile\Profile328\mscorlib.dll")

            };

            references.AddRange(Directory.GetFiles(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades").Select(f => MetadataReference.CreateFromFile(f)));

            var op = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
            //op.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default);
            //CSharpCompilationOptions.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default);
            CSharpCompilation compilation = CSharpCompilation.Create(
                assemblyName,
                syntaxTrees: new[] { syntaxTree },
                references: references,
                options: op);
            Assembly assembly = null;
            using (var ms = new MemoryStream())
            {
                EmitResult result = compilation.Emit(ms);

                if (!result.Success)
                {
                    IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
                        //diagnostic.IsWarningAsError ||
                        diagnostic.Severity == DiagnosticSeverity.Error);

                    foreach (Diagnostic diagnostic in failures)
                    {
                        Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
                    }
                }
                else
                {
                    ms.Seek(0, SeekOrigin.Begin);
                    assembly = Assembly.Load(ms.ToArray());                 
                }
        }

These are the errors I get

CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'.

CS0012: The type 'XmlReader' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xml, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'.

4
  • Have you tried adding an assembly redirect for those assemblies to your app.config? (And by extension to the *.exe.config)
    – Guvante
    Commented Mar 1, 2016 at 0:20
  • I cant get assembly redirect to do anything - also don't think that is the problem here. The problem is most likely how i setup the roslyn compiler.
    – sjkp
    Commented Mar 1, 2016 at 6:36
  • IIRC DesktopAssemblyIdentityComparer solved the issue for me once. Note however, that CSharpCompilationOptions is immutable as well... have you tried op = op.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default)?
    – m0sa
    Commented Mar 2, 2016 at 12:24
  • OMG @m0sa I'm such a tool, it is working now with your suggestion. Thanks a lot
    – sjkp
    Commented Mar 3, 2016 at 20:07

1 Answer 1

5

As you predicted in the comments DesktopAssemblyIdentityComparer is the solution. However, CSharpCompilationOptions is immutable, and the WithAssemblyIdentityComparer method returns a new instance, so you have to use it like this:

var op = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
op = op.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default);
CSharpCompilation compilation = CSharpCompilation.Create(
    assemblyName,
    syntaxTrees: new[] { syntaxTree },
    references: references,
    options: op);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.