Roslyn has no reference to System.Runtime

前端 未结 2 2000
面向向阳花
面向向阳花 2021-01-01 12:30

I\'m working on a project where we are using Roslyn to compile some templates for us. Now when I\'m compiling the template I\'m receiving multiple errors in the Compil

相关标签:
2条回答
  • 2021-01-01 12:44

    It appears that you are referencing a PortableClassLibrary. Portable Class Libraries pick up some of the basic types (like object/string/etc) from "System.Runtime.dll". However, in the desktop framework these come from mscorlib.dll. When you use typeof(object).Assembly, you get Roslyn's own version of object. Since Roslyn is not built as Portable Class Libraries, that is the one from mscorlib, and doesn't match the identity of your other references.

    0 讨论(0)
  • 2021-01-01 12:56

    By request of Dejan in the comment section, I'll post the answer (to my problem) as a real answer.


    I've found out what the problem was! The compiler was correct all along. A blog post of smack0007 triggered me of trying something else. Instead of using the Facade dll, try referencing the necessary dll's manually. The GetGlobalReferences method now looks like this:

    private IEnumerable<MetadataReference> GetGlobalReferences()
    {
        var assemblies = new [] 
            {
                /*Making sure all MEF assemblies are loaded*/
                typeof(System.Composition.Convention.AttributedModelProvider).Assembly, //System.Composition.AttributeModel
                typeof(System.Composition.Convention.ConventionBuilder).Assembly,   //System.Composition.Convention
                typeof(System.Composition.Hosting.CompositionHost).Assembly,        //System.Composition.Hosting
                typeof(System.Composition.CompositionContext).Assembly,             //System.Composition.Runtime
                typeof(System.Composition.CompositionContextExtensions).Assembly,   //System.Composition.TypedParts
    
                /*Used for the GeneratedCode attribute*/
                typeof(System.CodeDom.Compiler.CodeCompiler).Assembly,              //System.CodeDom.Compiler
            };
    
        var refs = from a in assemblies 
                    select new MetadataFileReference(a.Location);
        var returnList = refs.ToList();
    
        //The location of the .NET assemblies
        var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
    
        /* 
            * Adding some necessary .NET assemblies
            * These assemblies couldn't be loaded correctly via the same construction as above,
            * in specific the System.Runtime.
            */
        returnList.Add(new MetadataFileReference(Path.Combine(assemblyPath, "mscorlib.dll")));
        returnList.Add(new MetadataFileReference(Path.Combine(assemblyPath, "System.dll")));
        returnList.Add(new MetadataFileReference(Path.Combine(assemblyPath, "System.Core.dll")));
        returnList.Add(new MetadataFileReference(Path.Combine(assemblyPath, "System.Runtime.dll")));
    
        return returnList;
    }
    

    When decompiling the System.Runtime.dll I also saw why it couldn't be referenced in any other way. The dll is empty, it only contains some references to other assemblies. Therefore one can't reference this assembly in any other way.

    0 讨论(0)
提交回复
热议问题