【问题标题】:Dynamically compile multiple files into assembly using Rosyln使用 Rosyln 将多个文件动态编译成程序集
【发布时间】:2020-12-24 05:32:19
【问题描述】:

我最近看到使用 CSharpCodeProvider 在 .NET Core 5 中已被弃用。我想知道是否有一种聪明的方法可以将 多个文件 组合到一个 dll 中,我可以使用 Rosyln 从中加载.我不想坚持使用 .NET Framework 4.7,因为这是我正在开发的程序的重要组成部分。

我的代码过去差不多就是这样

string Files[] = Directory.GetFiles("xxxxx/bin/Debug/net5.0/Scripts", ".cs");
Parameters = new CompilerParameters(new string[1] {"xxxxx/bin/Debug/net5.0/Program.dll"}, "xxxxx/bin/Debug/net5.0/Scripts/Output/Scripts.dll");
return new CSharpCodeProvider().CompileAssemblyFromFile(Parameters, Files).CompiledAssembly;

在 Rosyln 中最有效的方法是什么?

参考:https://github.com/dotnet/runtime/issues/18768#issuecomment-265381303(PlatformNotSupportedException)

【问题讨论】:

    标签: c# .net-core compilation runtime roslyn


    【解决方案1】:

    Nick Polyak 写了一篇关于代码项目的完整文章:Using Roslyn for Compiling Code into Separate Net Modules and Assembling them into a Dynamic Library 和完整的源代码available on GitHub

    虽然这可能不是您正在寻找的内容,但它应该可以帮助您开始了解 Roslyn 概念,了解如何从 SyntaxTree 创建 CSharpCompilation 并将其发送到 Stream

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多