Unity ResolveAll泛型接口

本文关键字:泛型接口 ResolveAll Unity | 更新日期: 2023-09-27 18:09:15

我使用Unity IoC,我想注册非泛型类到泛型接口的映射。之后,我想使用ResolveAll方法来检索与泛型接口相关的所有注册。下面是示例代码:

interface ISample<out T> { }
class Ca : ISample<int> { }
class Cb : ISample<string> { }
class Program
{
    static void Main(string[] args)
    {
        var container = new UnityContainer();
        container.RegisterType<ISample<int>,Ca>();
        container.RegisterType<ISample<string>, Cb>();
        var classList = container.ResolveAll(typeof(ISample<>));
    }
}

在我的代码这行:

var classList = container.ResolveAll(typeof(ISample<>));

如何处理这个错误:

Resolution of the dependency failed, type = "ConsoleApplication1Unity.ISample`1[T][]", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - Could not execute the method because either the method itself or the containing type is not fully instantiated.
----------------------------------------------- At the time of the exception, the container was:
  Resolving ConsoleApplication1Unity.ISample`1[T][],(none)

Unity ResolveAll泛型接口

ResolveAll用于查找特定类型的所有命名解析,它不适用于像您正在使用的开放泛型。要得到你想要的,你需要做

var registrations = container.Registrations
                   .Where(x => x.RegisteredType.IsGenericType && 
                               x.RegisteredType.GetGenericTypeDefinition() == typeof(ISample<>))
                   .ToList();

这将给你一个所有注册的列表。要获得类对象的集合,您只需要在每个返回的注册时调用Resolve

var classList = new List<object>();
foreach (var registration in registrations)
{
    var classObject = container.Resolve(registration.RegisteredType, registration.Name);
    classList.Add(classObject);
}

唯一可以同时容纳ISample<int>ISample<string>List<T>类型是objectList<ISample<object>>行不通。如果您将接口重写为

interface ISample { }
interface ISample<out T> : ISample { }

它使代码简单了很多,你在列表中得到了一个更好的对象,它可以让你访问不依赖于TISample的属性和方法。

var registrations = container.Registrations
                   .Where(x => typeof(ISample).IsAssignableFrom(x.RegisteredType));
var classList = new List<ISample>();
foreach (var registration in registrations)
{
    var classObject = container.Resolve(registration.RegisteredType, registration.Name);
    classList.Add((ISample)classObject);
}

注:只是为了清楚地说明ResolveAll是做什么的,它基本上是

public IEnumerable<object> ResolveAll(Type t, params ResolverOverride[] resolverOverrides)
{
    var registrations = this.Registrations.Where(x => x.RegisteredType == t);
    foreach (var registration in registrations)
    {
        if(registration.Name != null)
            yield return this.Resolve(registration.RegisteredType, registration.Name, resolverOverrides)
    }
}

ISample<>不是Unity的有效类型;你不能注册它,也不能使用ResolveAll让所有类型使用它。如果您查看ResolveAll返回的内容,就会发现问题所在。在这种情况下,它将返回一个无效的IEnumerable>。

我不太确定你想做什么,所以我不知道该建议你尝试什么

我已经完成了创建以下ExtensionMethod的任务

public static IEnumerable<object> ResolveAllOpenGeneric(this IUnityContainer container, Type openGenericType)
{
    if (!openGenericType.IsGenericTypeDefinition)
        throw new ArgumentNullException("argument must be open generic type");
    return container.Registrations.Where(c =>
                                            c.RegisteredType.IsGenericType
                                            && c.RegisteredType.GetGenericTypeDefinition() == openGenericType
                                        )
                                        .Select(r =>
                                                    container.Resolve(r.RegisteredType, r.Name)
                                        );
}
public static IEnumerable<T> ResolveAllOpenGeneric<T>(this IUnityContainer container, Type openGenericType)
{
    if (!openGenericType.IsGenericTypeDefinition)
        throw new ArgumentNullException("argument must be open generic type");
    return container.Registrations.Where(c =>
                                             c.RegisteredType.IsGenericType
                                             && c.RegisteredType.GetGenericTypeDefinition() == openGenericType
                                         )
                                         .Select(r =>
                                                     (T)container.Resolve(r.RegisteredType, r.Name)
                                         );
}