gpt4 book ai didi

c# - 当引用的程序集同时引用 mscorlib 2.0.5.0 和 4.0.0.0 时如何让 roslyn 进行编译

转载 作者:太空狗 更新时间:2023-10-29 23:13:23 24 4
gpt4 key购买 nike

我正在动态构建一个使用 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: 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'.

最佳答案

正如您在评论中预测的那样,DesktopAssemblyIdentityComparer 是解决方案。但是,CSharpCompilationOptions 是不可变的,WithAssemblyIdentityComparer 方法返回一个新实例,因此您必须像这样使用它:

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

关于c# - 当引用的程序集同时引用 mscorlib 2.0.5.0 和 4.0.0.0 时如何让 roslyn 进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35711461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com