C# Unity容器能否动态加载程序集?

C# Unity容器能否动态加载程序集?,c#,unity-container,C#,Unity Container,我正在开发一个应用程序。我希望unity解析我的类型,而不必引用主项目中的程序集。我认为它通过使用配置类型注册来自动加载程序集,但除非我添加对包含依赖项的程序集的引用,否则它似乎不起作用 是否存在从当前目录中的程序集加载类型的wat 谢谢 听起来你想要的是MEF而不是团结。MEF是为动态发现而设计的 阅读此问题的答案:现在提供帮助可能有点晚了,但是如果使用XML配置方法,Unity可以动态加载程序集,注册每个程序集,然后相应地注册类型。我已经使用这个过程对一个非常依赖DI的框架进行了一段时间的小

我正在开发一个应用程序。我希望unity解析我的类型,而不必引用主项目中的程序集。我认为它通过使用配置类型注册来自动加载程序集,但除非我添加对包含依赖项的程序集的引用,否则它似乎不起作用

是否存在从当前目录中的程序集加载类型的wat


谢谢

听起来你想要的是MEF而不是团结。MEF是为动态发现而设计的


阅读此问题的答案:

现在提供帮助可能有点晚了,但是如果使用XML配置方法,Unity可以动态加载程序集,注册每个程序集,然后相应地注册类型。我已经使用这个过程对一个非常依赖DI的框架进行了一段时间的小扩展


如果遇到Unity无法解析在主应用程序中注册但在另一个未引用程序集中定义的类型的问题,并且引用所述程序集可以解决该问题,则很可能意味着该类型没有复制到应用程序的输出目录。默认情况下,仅自动复制直接引用的部件。如果手动复制或通过生成后事件复制,或者重定向生成路径,以便将未引用的程序集生成到应用程序的输出目录,则应能正常工作。

我知道这是不久前提出的问题,但对于任何寻求答案的人,必须确保Unity可以在运行时找到程序集。因此,您需要将它们放在GAC中,或者将程序集dll放在与可执行文件相同的目录中。如果您对依赖项使用不同的目录,并且有web应用程序,则必须在web.config中设置探测元素:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="Dependencies" />
    </assemblyBinding>
</runtime>

对于正在寻找如何解决此问题的代码的任何人,以下代码将查找定义目录中的所有程序集,并向Unity注册它们:

string DependenciesPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Dependencies");
string[] Dependencies   = Directory.GetFiles(DependenciesPath, DLL_PATTERN);

Dictionary<Type, Type> pluginMappings = new Dictionary<Type, Type>();

//Load Dependency Assemblies
foreach (string fileName in Dependencies)
{
    string assemblyName = Path.GetFileNameWithoutExtension(fileName);
    if (assemblyName != null)
    {
        Assembly pluginAssembly = Assembly.Load(assemblyName);

        foreach (Type pluginType in pluginAssembly.GetTypes())
        {
            if (pluginType.IsPublic) //Only look at public types
            {
                if (!pluginType.IsAbstract)  //Only look at non-abstract types
                {
                    //Gets a type object of the interface we need the plugins to match
                    Type[] typeInterfaces = pluginType.GetInterfaces();
                    foreach (Type typeInterface in typeInterfaces)
                    {
                        if (pluginMappings.ContainsKey(typeInterface))
                        {
                            throw new DuplicateTypeMappingException(typeInterface.Name, typeInterface, pluginMappings[typeInterface], pluginType);
                        }
                        pluginMappings.Add(typeInterface, pluginType);
                    }
                }
            }
        }
    }
}

//Web API resolve dependencies with Unity
IUnityContainer container = new UnityContainer();
foreach (var mapping in pluginMappings)
{
    container.RegisterType(mapping.Key, mapping.Value);
}
string DependenciesPath=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,“依赖项”);
string[]Dependencies=Directory.GetFiles(DependenciesPath,DLL\u模式);
Dictionary pluginMappings=新字典();
//加载依赖项程序集
foreach(依赖项中的字符串文件名)
{
string assemblyName=Path.GetFileNameWithoutExtension(文件名);
if(assemblyName!=null)
{
Assembly pluginAssembly=Assembly.Load(assemblyName);
foreach(在pluginAssembly.GetTypes()中键入pluginType)
{
if(pluginType.IsPublic)//只查看公共类型
{
if(!pluginType.isastract)//只查看非抽象类型
{
//获取我们需要插件匹配的接口的类型对象
Type[]typeInterfaces=pluginType.GetInterfaces();
foreach(类型接口中的类型接口)
{
if(插件文件容器(类型接口))
{
抛出新的DuplicateTypeMappingException(typeInterface.Name、typeInterface、pluginMappings[typeInterface],pluginType);
}
pluginMappings.Add(类型接口,pluginType);
}
}
}
}
}
}
//Web API使用Unity解决依赖关系
IUnityContainer容器=新的UnityContainer();
foreach(插件映射中的var映射)
{
RegisterType(mapping.Key,mapping.Value);
}

您正在使用xml配置吗?