从封闭泛型转换为开放泛型 [英] Converting from closed generics to open generics
本文介绍了从封闭泛型转换为开放泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种方法将以下封闭泛型语句转换为开放泛型语句,即我不想对用户,雇主等实体重复相同的操作。
使用用户的封闭类型:
UnityContainer.RegisterType< Func< IDataContextAdapter,IRepository<用户>>>(
new InjectionFactory(c =>
new Func< IDataContextAdapter,IRepository< User>>(
context => new Repository< User>(context))
)
);
我尝试通过应用typeof(..)操作转换为打开泛型,但没有太多成功。
任何想法?
解决方案
制作泛型函数
< $ p
$ b $ UnityContainer.RegisterType< Func< IDataContextAdapter,IRepository< T>>>(pre $ p $
public void Bla< T> b $ b new InjectionFactory(c =>
new Func< IDataContextAdapter,IRepository< T>>(
context => new Repository< T>(context))
)
);
}
I am looking for a way to convert the following closed generics statement to open generic statement, i.e. I don't want repeat the same for entities like User, Employer, etc.
Closed type using User:
UnityContainer.RegisterType<Func<IDataContextAdapter, IRepository<User>>>(
new InjectionFactory(c =>
new Func<IDataContextAdapter, IRepository<User>>(
context => new Repository<User>(context))
)
);
I tried converting to Open generics by applying typeof(..) operation, but didn't had much success.
Any Ideas?
解决方案
Make generic Function
public void Bla<T>() where T:class
{
UnityContainer.RegisterType<Func<IDataContextAdapter, IRepository<T>>>(
new InjectionFactory(c =>
new Func<IDataContextAdapter, IRepository<T>>(
context => new Repository<T>(context))
)
);
}
这篇关于从封闭泛型转换为开放泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文