Unity TIP4: 带泛型参数的接口注入(interface,generic)
2013-11-27 14:03
393 查看
一:第一类需求
接口和类定义如下:
要求用Unity注入。
1:运行时注入
如果是运行时注入,可如下编码:
2:配置文件配置注入
如果是在配置文件中配置,配置文件如下:
编码如下:
二:第二类需求
接口和类定义如下:
1:运行时注入
如果是运行时注入,可如下编码:
2:配置文件配置注入
如果是在配置文件中配置,配置文件如下:
编码如下:
三:总结
可以看到,如果是运行时注入,那么Resolve方法中,必须指定泛型的类型。
如果是在配置文件中,为了表明这是个带泛型参数的接口或者类,则需要为其指定后缀“`”。
接口和类定义如下:
public interface ISample<T> {} public class Sample : ISample<SomeClass2> {} public class SomeClass2{}
要求用Unity注入。
1:运行时注入
如果是运行时注入,可如下编码:
using (IUnityContainer container = new UnityContainer()) { ConfigurationManager.GetSection("unity"); UnityConfigurationSection.CurrentSection.Configure(container, "containerOne"); container.RegisterType<ISample<SomeClass2>, Sample>(); var re = container.Resolve<ISample<SomeClass2>>(); }
2:配置文件配置注入
如果是在配置文件中配置,配置文件如下:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <alias alias="ISample`1" type="ISample`1" /> <alias alias="Sample" type="Sample" /> <container name="containerOne"> <register type="ISample`1" name="Sample" mapTo="Sample" /> </container> </unity>
编码如下:
using (IUnityContainer container = new UnityContainer()) { ConfigurationManager.GetSection("unity"); UnityConfigurationSection.CurrentSection.Configure(container, "containerOne"); var re = container.Resolve<ISample<SomeClass2>>("Sample"); }
二:第二类需求
接口和类定义如下:
public interface ISample<T> {} public class Sample<T> : ISample<T> { } public class SomeClass2{}
1:运行时注入
如果是运行时注入,可如下编码:
using (IUnityContainer container = new UnityContainer()) { ConfigurationManager.GetSection("unity"); UnityConfigurationSection.CurrentSection.Configure(container, "containerOne"); container.RegisterType<ISample<SomeClass2>, Sample<SomeClass2>>(); var re = container.Resolve<ISample<SomeClass2>>(); }
2:配置文件配置注入
如果是在配置文件中配置,配置文件如下:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <alias alias="ISample`1" type="ISample`1" /> <alias alias="Sample`1" type="Sample`1" /> <container name="containerOne"> <register type="ISample`1" name="Sample" mapTo="Sample`1" /> </container> </unity>
编码如下:
using (IUnityContainer container = new UnityContainer()) { ConfigurationManager.GetSection("unity"); UnityConfigurationSection.CurrentSection.Configure(container, "containerOne"); var re = container.Resolve<ISample<SomeClass2>>("Sample"); }
三:总结
可以看到,如果是运行时注入,那么Resolve方法中,必须指定泛型的类型。
如果是在配置文件中,为了表明这是个带泛型参数的接口或者类,则需要为其指定后缀“`”。
相关文章推荐
- Unity TIP4: 带泛型参数的接口注入(interface,generic)
- 通过泛型参数实现接口注入
- 编写高质量代码改善C#程序的157个建议——建议42:使用泛型参数兼容泛型接口的不可变性
- 编写高质量代码改善C#程序的157个建议——建议43:让接口中的泛型参数支持协变
- List方法及去重,泛型(方法,类,接口),泛型通配符,迭代器,可变参数,set,map,Collections工具类,Comparator
- [疯狂Java]泛型:类型参数多态问题、类型通配符(?)、类型通配符的上下限、类型参数的上限(类、接口)
- 通过实现HandlerMethodArgumentResolver接口,给springMvc的Controller的方法注入自定义参数
- [译]委托和接口泛型参数类型的协变和逆变
- Swift 泛型参数(Generic Parameters and Arguments)
- 为参数类型一样返回类型不同的接口写一个泛型方法
- 使用泛型 类型System.Collections.Generic.KeyValuePair需要2个类型参数
- 泛型类、多参数泛型类、泛型接口(2)、静态非静态泛型函数、泛型绑定类
- Editlog的接口规范设计文档(HDFS-1580:Add interface for generic Write Ahead Logging mechanism)
- 建议43:让接口中的泛型参数支持协变
- Wal log的接口规范设计文档(HDFS-1580:Add interface for generic Write Ahead Logging mechanism)
- java反射获得泛型参数getGenericSuperclass()
- Java -- 获取实现接口的泛型的参数化类型和实际类型参数
- spring mvc Restful接口传入多参数的问题和解决方案(参数全部注入第一个变量bug)
- 在范型中接口和实例间转换的问题Why No? .net 2.0 Generic cast Type From Instance to Interface
- 使用泛型 类型“System.Collections.Generic.IEnumerator<T>”需要 1 个类型参数