简体   繁体   English

使用Unity,如何使用通用接口自动调整泛型类,而无需向其注册每种类型

[英]Using Unity, How do I autoregister a generic class with a generic interface without registering EVERY type to it

I am using Unity and Unity.AutoRegistration . 我正在使用Unity和Unity.AutoRegistration This line for Unity: Unity的这一行:

unityContainer.RegisterType(typeof(IAction<>), typeof(Action<>));

effectively registers every class in the project to IAction/Action: 有效地将项目中的每个类注册到IAction / Action:

unityContainer.RegisterType<IAction<ObjectA>, Action<ObjectA>>();
unityContainer.RegisterType<IAction<ObjectB>, Action<ObjectB>>();
unityContainer.RegisterType<IAction<ObjectC>, Action<ObjectC>>();
[...]
unityContainer.RegisterType<IAction<UnrelatedObject>, Action<UnrelatedObject>>();
[...]

But, I only want specific objects to be registered. 但是,我只想要注册特定的对象。 How would I do that? 我该怎么办? My guess is to add a custom attribute decorator to the specific classes. 我的猜测是为特定的类添加一个自定义属性装饰器。

[ActionAtribute]
public class ObjectB
{ [...] }

And try to use Unity.AutoRegistration . 并尝试使用Unity.AutoRegistration This is where I am stuck at: 这是我被困在的地方:

unityContainer.ConfigureAutoRegistration()
    .Include(If.DecoratedWith<ActionAtribute>,
             Then.Register()
             .As   ?? // I'm guessing this is where I specify
             .With ?? // IAction<match> goes to Action<match>
             )
    .ApplyAutoRegistration();

Include method has overload that allows you to pass lambda to register your type. Include方法具有重载,允许您传递lambda以注册您的类型。 To achieve exactly what you want with attributes you can do like this: 要使用属性实现您想要的功能,您可以这样做:

        unityContainer.ConfigureAutoRegistration()
            .Include(If.DecoratedWith<ActionAtribute>,
                     (t, c) => c.RegisterType(typeof(IAction<>).MakeGenericType(t), typeof(Action<>).MakeGenericType(t)))
            .IncludeAllLoadedAssemblies()
            .ApplyAutoRegistration();

Also, first argument of Include method is Predicate, so if you don't want to use attributes but some other mechanism to define what types to include or exclude, you can do like this: 此外,Include方法的第一个参数是Predicate,因此如果您不想使用属性而是使用其他机制来定义要包含或排除的类型,您可以这样做:

        // You may be getting these types from your config or from somewhere else
        var allowedActions = new[] {typeof(ObjectB)}; 
        unityContainer.ConfigureAutoRegistration()
            .Include(t => allowedActions.Contains(t),
                     (t, c) => c.RegisterType(typeof(IAction<>).MakeGenericType(t), typeof(Action<>).MakeGenericType(t)))
            .IncludeAllLoadedAssemblies()
            .ApplyAutoRegistration();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 向Unity注册通用处理程序接口 - Registering Generic Handler Interface with Unity 如何在接口中参数化然后在实现的类中指定泛型 - How do I parameterise in an interface and then specify generic type in implemented class 如何使用反射确定已实现接口的通用类型? - How do I determine the generic type of an implemented interface using reflection? 如何实现对通用接口有约束的通用类? - How do I implement a generic class that has an constraint for a generic interface? 如何在具有相同数据类型参数的另一个泛型类(或接口)中使用泛型类(或接口)而无需强制转换 - How to use generic class (or interface) in another generic class (or interface) with the same data type parameters without casting 如何在通用类声明中使用通用类型? - How do I use a generic type within a generic class declaration? 如何在泛型类中设置泛型类型 - How do I set a generic type inside a generic class Unity注册非泛型接口的泛型类型 - Unity register generic type for non generic interface 在泛型类中使用泛型接口 - Using generic interface in a generic class 从接口序列化通用类时,如何让DataContractJsonSerializer在类型提示中使用具体类型 - How do I get DataContractJsonSerializer to use concrete type in type hint when serializing generic class from interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM