Register null as instance in Unity container

前端 未结 5 1362
北恋
北恋 2020-12-17 10:08

I have a repository class with optional dependency:

class MyRepository : BaseRepository, IMyRepository
{
    public MyRepository(IDataContext dataContext, IC         


        
5条回答
  •  清歌不尽
    2020-12-17 10:47

    I found that RegisterType, instead of Register instance, supports returning null.

    container.RegisterType(new InjectionFactory((c) => null));
    

    This was the most straightforward way of getting an actual null to be returned.

提交回复
热议问题