C# 动态注册DLL后如何与Unity一起使用DI
我向Unity动态注册DLLC# 动态注册DLL后如何与Unity一起使用DI,c#,asp.net,unity-container,C#,Asp.net,Unity Container,我向Unity动态注册DLL public static void RegisterTypes(IUnityContainer container) { string dependenciesPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SpecificDll"); string[] dependencies = Directory.GetFiles(dependenciesPath); Dicti
public static void RegisterTypes(IUnityContainer container)
{
string dependenciesPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SpecificDll");
string[] dependencies = Directory.GetFiles(dependenciesPath);
Dictionary<Type, Type> pluginMappings = new Dictionary<Type, Type>();
//Load Dependency Assemblies
foreach (string fileName in dependencies)
{
//Get type interface and plugin type
....
}
foreach (var mapping in pluginMappings)
{
container.RegisterType(mapping.Key, mapping.Value);
}
}
但我不知道动态加载程序集时该怎么做
你能帮我吗
最后,我创建了第三个具有通用接口的项目 在我的Dll中:
public class ServiceSpe : IServiceSpe
{
public string GetLabel()
{
return "hello world";
}
}
在我的共同项目中:
public interface IServiceSpe
{
string GetLabel();
}
在我的控制器中:
public MyController()
{
}
public MyController(IServiceSpe serviceSpe)
{
if (serviceSpe != null)
{
string result = serviceSpe.GetLabel();
}
}
如果依赖项包含具有接口“IServiceSpe”的类:
MyController(iSeries服务SPE)被称为
否则
调用MyController()
public interface IServiceSpe
{
string GetLabel();
}
public MyController()
{
}
public MyController(IServiceSpe serviceSpe)
{
if (serviceSpe != null)
{
string result = serviceSpe.GetLabel();
}
}