当引用的程序集引用mscorlib 2.0.5.0和4.0.0.0时,如何让roslyn编译

时间:2016-02-29 22:59:40

标签: c# .net roslyn

我正在动态构建一个使用Microsoft.OData.Client的类,这个库引用了mscorlib 2.0.5.0(我猜PCL)和4.0.0.0。 enter image description here

我想用roslyn编译我的课程,作为一个更大的课程的一部分,但我似乎无法让它工作。我的Roslyn编译器代码非常简约

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());                 
                }
        }

这些是我得到的错误

  

CS0012:类型&#39;对象&#39;在未引用的程序集中定义。您必须添加对assembly&m; mscorlib的引用,Version = 2.0.5.0,Culture = neutral,PublicKeyToken = 7cec85d7bea7798e,Retargetable = Yes&#39;。

     

CS0012:类型&#39; XmlReader&#39;在未引用的程序集中定义。您必须添加对程序集的引用,System.Xml,Version = 2.0.5.0,Culture = neutral,PublicKeyToken = 7cec85d7bea7798e,Retargetable = Yes&#39;。

1 个答案:

答案 0 :(得分:4)

正如您在评论中所预测的那样$(".uk-margin").removeData("accordion"); UIkit.accordion($(".uk-margin")); 是解决方案。但是,DesktopAssemblyIdentityComparer是不可变的,CSharpCompilationOptions方法返回一个新实例,所以你必须像这样使用它:

WithAssemblyIdentityComparer