You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.2 KiB
C#

using Admin.Core.Common;
using Admin.Core.RunPlc;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Linq;
using System.Reflection;
namespace Admin.Core.Extensions
{
/// <summary>
/// PLC服务
/// </summary>
public static class PlcSetup
{
public static void AddPlcSetup(this IServiceCollection services)
{
services.AddSingleton<IRunPlcService, RunPlcService>();
var baseType = typeof(IRunPlcService);
var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
var referencedAssemblies = System.IO.Directory.GetFiles(path, "Admin.Core.RunPlc.dll").Select(Assembly.LoadFrom).ToArray();
var types = referencedAssemblies
.SelectMany(a => a.DefinedTypes)
.Select(type => type.AsType())
.Where(x => x != baseType && baseType.IsAssignableFrom(x)).ToArray();
var implementTypes = types.Where(x => x.IsClass).ToArray();
foreach (var implementType in implementTypes)
{
services.AddTransient(implementType);
}
}
}
}