Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态注册DLL后如何与Unity一起使用DI_C#_Asp.net_Unity Container - Fatal编程技术网

C# 动态注册DLL后如何与Unity一起使用DI

C# 动态注册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

我向Unity动态注册DLL

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();
        }
    }