3 Star 13 Fork 5

faib920/fireasy3

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
CSharpCodeCompiler.cs 3.34 KB
一键复制 编辑 原始数据 按行查看 历史
faib920 提交于 2年前 . 完善
// -----------------------------------------------------------------------
// <copyright company="Fireasy"
// email="faib920@126.com"
// qq="55570729">
// (c) Copyright Fireasy. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using System.IO;
using System.Reflection;
using System.Text;
namespace Fireasy.Common.Compiler
{
/// <summary>
/// CSharp 代码编译器。无法继承此类。
/// </summary>
public sealed class CSharpCodeCompiler : ICodeCompiler
{
/// <summary>
/// 编译代码生成一个程序集。
/// </summary>
/// <param name="sources">程序源代码。</param>
/// <param name="options">配置选项。</param>
/// <returns>由代码编译成的程序集。</returns>
public Assembly? CompileAssembly(IEnumerable<string> sources, ConfigureOptions? options = null)
{
var dotnetPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
Func<string, MetadataReference> parser = path => File.Exists(path) ?
MetadataReference.CreateFromFile(path) :
MetadataReference.CreateFromFile(Path.Combine(dotnetPath, path));
options ??= new ConfigureOptions();
var assemblyName = string.IsNullOrWhiteSpace(options.AssemblyName) ? Guid.NewGuid().ToString() : options.AssemblyName;
var compilation = CSharpCompilation.Create(assemblyName)
.AddSyntaxTrees(sources.Select(s => CSharpSyntaxTree.ParseText(s)))
.AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location))
.AddReferences(options.Assemblies.Select(parser))
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Release));
if (!string.IsNullOrEmpty(options.OutputAssembly))
{
var result = compilation.Emit(options.OutputAssembly);
if (result.Success)
{
return Assembly.LoadFrom(options.OutputAssembly);
}
else
{
ThrowCompileException(result);
return null;
}
}
else
{
using var ms = new MemoryStream();
var result = compilation.Emit(ms);
if (result.Success)
{
return Assembly.Load(ms.ToArray());
}
else
{
ThrowCompileException(result);
return null;
}
}
}
private void ThrowCompileException(EmitResult result)
{
var errorBuilder = new StringBuilder();
foreach (var diagnostic in result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error))
{
errorBuilder.AppendFormat("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
}
throw new CodeCompileException(errorBuilder.ToString());
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/faib920/fireasy3.git
git@gitee.com:faib920/fireasy3.git
faib920
fireasy3
fireasy3
dev3.0

搜索帮助