Converting from closed generics to open generics

倾然丶 夕夏残阳落幕 提交于 2020-01-15 23:25:48

问题


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?


回答1:


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))
        )
 );
}


来源:https://stackoverflow.com/questions/5344195/converting-from-closed-generics-to-open-generics

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!