55 lines
2.0 KiB
C#
55 lines
2.0 KiB
C#
|
|
using Autofac;
|
|||
|
|
using CirculateTool;
|
|||
|
|
using WcsMain.WcsAttribute.AutoFacAttribute;
|
|||
|
|
|
|||
|
|
namespace WcsMain.Plugins;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 新的 WCS 定时任务类,添加 IOC 容器支持,添加非静态变量支持
|
|||
|
|
/// </summary>
|
|||
|
|
[Component]
|
|||
|
|
public class WcsCirculation(IComponentContext componentContext) : StartCirculation
|
|||
|
|
{
|
|||
|
|
private readonly IComponentContext _componentContext = componentContext;
|
|||
|
|
|
|||
|
|
public override void StartTask(Type type, object[]? instanceParams = null)
|
|||
|
|
{
|
|||
|
|
object? instance = null;
|
|||
|
|
var methods = type.GetMethods();
|
|||
|
|
foreach (var method in methods)
|
|||
|
|
{
|
|||
|
|
var attributes = method.GetCustomAttributes(false);
|
|||
|
|
foreach (var attribute in attributes)
|
|||
|
|
{
|
|||
|
|
if (attribute is not CirculationAttribute needDurable) continue;
|
|||
|
|
string methodDescription = needDurable.MethodDescription ?? $"{type.Name}.{method.Name}";
|
|||
|
|
instance ??= CreateInstance(type);
|
|||
|
|
bool Action() => (bool)(method.Invoke(instance, []) ?? false);
|
|||
|
|
StartTask(Action, methodDescription, needDurable.CirculationTime);
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
private object? CreateInstance(Type type)
|
|||
|
|
{
|
|||
|
|
var constructors = type.GetConstructors();
|
|||
|
|
var constructorList = constructors.ToList();
|
|||
|
|
constructorList = [.. constructorList.OrderByDescending(s => s.GetParameters().Length)];
|
|||
|
|
foreach (var constructor in constructorList)
|
|||
|
|
{
|
|||
|
|
var parameters = constructor.GetParameters();
|
|||
|
|
if (parameters.Length == 0) return Activator.CreateInstance(type);
|
|||
|
|
List<object> arguments = [];
|
|||
|
|
foreach (var parameter in parameters)
|
|||
|
|
{
|
|||
|
|
object par = _componentContext.Resolve(parameter.ParameterType);
|
|||
|
|
arguments.Add(par);
|
|||
|
|
}
|
|||
|
|
return Activator.CreateInstance(type, [.. arguments]);
|
|||
|
|
}
|
|||
|
|
return Activator.CreateInstance(type);
|
|||
|
|
}
|
|||
|
|
}
|