Resolving IEnumerable with Unity

前端 未结 8 1097
梦如初夏
梦如初夏 2020-12-04 22:05

Can Unity automatically resolve IEnumerable?

Let\'s say I have a class with this constructor:

public CoalescingParserSelector(I         


        
8条回答
  •  甜味超标
    2020-12-04 22:21

    I did this like so

            container.RegisterTypes(
                AllClasses.FromLoadedAssemblies().
                    Where(type => typeof(IParserBuilder).IsAssignableFrom(type)),
                WithMappings.FromAllInterfaces,
                WithName.TypeName,
                WithLifetime.Transient);
    
            container.RegisterType, IParserBuilder[]>();
    

    Which registers everything that implements IParserBuilder, so when you add a new one you don't need to add any other code for it to appear in the list of builders.

提交回复
热议问题