gpt4 book ai didi

c# - 使用 Roslyn 编译时自动解决依赖关系

转载 作者:行者123 更新时间:2023-11-30 17:35:04 26 4
gpt4 key购买 nike

我目前正在编写一个应用程序,该应用程序当前通过 Roslyn 的工作区 API 加载项目,将指定的 C# 文件转换为语法树,然后在内存中创建程序集,然后最终提取 IL。

一切正常,但是一旦我在所述 C# 文件中引用任何外部库,编译就会失败,因为 Roslyn 不知道在哪里解析这些引用。

这是我目前正在做的事情的简化版本:

MetadataReference[] metaDatareferences = {    MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),    MetadataReference.CreateFromFile(typeof(Uri).GetTypeInfo().Assembly.Location),    MetadataReference.CreateFromFile(typeof(DynamicAttribute).GetTypeInfo().Assembly.Location),    MetadataReference.CreateFromFile(typeof(AssemblyMetadata).GetTypeInfo().Assembly.Location),};var sourceLanguage = new CSharpLanguage();var syntaxTree = sourceLanguage.ParseText(sourceCode, SourceCodeKind.Regular);var options = new CSharpCompilationOptions(    OutputKind.DynamicallyLinkedLibrary,    optimizationLevel: OptimizationLevel.Debug,    allowUnsafe: true);CSharpCompilation compilation = CSharpCompilation.Create("ExampleAssembly", options: options);var stream = new MemoryStream();var result = compilation.    AddReferences(metaDatareferences)    .AddSyntaxTrees(syntaxTree)    .Emit(stream);// Success is falseif (!emitResult.Success){    foreach (var diagnostic in emitResult.Diagnostics)    {        Debug.WriteLine(diagnostic.ToString());    }}

Debug.WriteLine 的输出是:

(1,7): error CS0246: The type or namespace name 'MediatR' could not be found (are you missing a using directive or an assembly reference?)(9,32): error CS0246: The type or namespace name 'Mediator' could not be found (are you missing a using directive or an assembly reference?)

我的 Roslyn 项目正在读取的文件很简单:

using MediatR;namespace ConsoleApplication{    public class Program    {        public static void Main(string[] args)        {            var mediator = new Mediator(null, null);        }    }}

我的问题是,Roslyn 是否提供 API 来自动加载文件可能具有的任何依赖项?我希望 Roslyn 工作区能够完成这项工作,但我一直找不到任何东西。

最佳答案

如果 MediatR 控制台项目是 project.json 项目,那么您可以使用 "Microsoft.DotNet.ProjectModel.Workspaces": "1.0 .0-preview2-1-003177"。您可以将它指向您的 project.json 并获得一个 Compilation 对象,这将为您完成获取项目引用、文件引用等的所有艰苦工作。 .. 然后你就可以从这里发出你的 IL。

这是一个例子:

var compilation = new ProjectJsonWorkspace(@"PathToYour\project.json").CurrentSolution.Projects.First().GetCompilationAsync().Result;

var stream = new MemoryStream();
var emitResult = compilation.Emit(stream);

或者如果您需要完全控制,您可以继续使用 CSharpCompilation.Create,从此处的 compilation 对象中复制您需要的内容,并传入一个 语法树

希望对您有所帮助。

关于c# - 使用 Roslyn 编译时自动解决依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41565111/

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