环境接口新增了加载项目文件路径,方便类库在Init事件中自动加载具体的依赖

This commit is contained in:
fengjiayi
2025-03-17 10:14:18 +08:00
parent 41cf0064f6
commit 26e88aea77
29 changed files with 273 additions and 23 deletions

View File

@@ -663,6 +663,12 @@ namespace Serein.Library.Api
/// </summary>
string EnvName { get; }
/// <summary>
/// 项目文件位置
/// </summary>
string ProjectFileLocation { get; }
/// <summary>
/// 是否全局中断
/// </summary>
@@ -699,6 +705,7 @@ namespace Serein.Library.Api
/// </summary>
UIContextOperation UIContextOperation { get; }
#endregion
#region
@@ -1033,6 +1040,7 @@ namespace Serein.Library.Api
/// </summary>
/// <param name="assemblyFullName">程序集的名称</param>
bool TryUnloadLibrary(string assemblyFullName);
/// <summary>
/// 运行时加载
/// </summary>

View File

@@ -27,6 +27,7 @@ namespace Serein.Library
public ISereinIOC IOC => sereinIOC;
public string EnvName => throw new NotImplementedException();
public string ProjectFileLocation => throw new NotImplementedException();
public bool IsGlobalInterrupt => throw new NotImplementedException();

View File

@@ -75,6 +75,7 @@ namespace Serein.Library
{
// 设置获取中断的委托
_getInterruptTask = () => NodeModel.Env.IOC.Get<FlowInterruptTool>().WaitTriggerAsync(NodeModel.Guid);
}
else if (!state)
{

View File

@@ -449,7 +449,6 @@ namespace Serein.Library
/// <summary>
/// 更新节点数据,并检查监视表达式是否生效
/// </summary>

View File

@@ -38,6 +38,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="9.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

View File

@@ -0,0 +1,100 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Serein.Library.Utils
{
/// <summary>
/// 动态编译
/// </summary>
public class DynamicCompiler
{
private readonly HashSet<MetadataReference> _references = new HashSet<MetadataReference>();
public DynamicCompiler()
{
// 默认添加当前 AppDomain 加载的所有程序集
var defaultReferences = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => !string.IsNullOrEmpty(a.Location)) // a.IsDynamic 动态程序集
.Select(a => MetadataReference.CreateFromFile(a.Location));
//AddReference(this.GetType());
_references.UnionWith(defaultReferences);
}
/// <summary>
/// 添加依赖程序集(通过类型)
/// </summary>
/// <param name="type">类型所在的程序集</param>
public void AddReference(Type type)
{
var assemblyLocation = type.Assembly.Location;
if (!string.IsNullOrEmpty(assemblyLocation))
{
_references.Add(MetadataReference.CreateFromFile(assemblyLocation));
}
}
/// <summary>
/// 添加依赖程序集(通过文件路径)
/// </summary>
/// <param name="assemblyPath">程序集文件路径</param>
public void AddReference(string assemblyPath)
{
if (File.Exists(assemblyPath))
{
_references.Add(MetadataReference.CreateFromFile(assemblyPath));
}
}
/// <summary>
/// 编译 C# 代码并返回程序集
/// </summary>
/// <param name="code">C# 代码文本</param>
/// <param name="assemblyName">程序集名称(可选)</param>
/// <returns>成功返回 Assembly失败返回 null</returns>
public Assembly Compile(string code, string assemblyName = null)
{
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
if(assemblyName is null)
{
assemblyName = Path.GetRandomFileName(); // 生成随机程序集名称
}
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
new[] { syntaxTree },
_references,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
using (var ms = new MemoryStream())
{
EmitResult result = compilation.Emit(ms);
if (!result.Success)
{
Console.WriteLine("编译失败:");
foreach (var diagnostic in result.Diagnostics)
{
Console.WriteLine(diagnostic.ToString());
}
return null;
}
ms.Seek(0, SeekOrigin.Begin);
return Assembly.Load(ms.ToArray());
}
}
}
}

View File

@@ -75,7 +75,6 @@ namespace Serein.Library.Utils
/// <summary>
/// 使用 ObjectPool 来复用 TriggerResult 对象
/// </summary>
// 示例 TriggerResult 对象池
public class TriggerResultPool<TResult>
{
private readonly ConcurrentExpandingObjectPool<TriggerResult<TResult>> _objectPool;

View File

@@ -54,6 +54,11 @@ namespace Serein.Library.Utils.SereinExpression
/// <exception cref="NotSupportedException"></exception>
public static object Evaluate(string expression, object targetObJ, out bool isChange)
{
if (expression.Equals("@get", StringComparison.OrdinalIgnoreCase))
{
isChange = false;
return targetObJ;
}
//if (expression is null || targetObJ is null)
//{
// throw new Exception("表达式条件expression is null、 targetObJ is null");