Search code examples
dependency-injectiondryioc

Exclude type from registration in DryIoc


How can I tell DryIoc to never register a specific type?

I have tried to use a serviceTypeCondition parameter in RegisterMany:

registrator.RegisterMany(new[] { typeof(DemoClass).GetAssembly() }, 
    serviceTypeCondition: (type) =>
    {
        var name = type.Name;
        return name != "SkipMyName";
    }

But looks like DryIoc still tries to read/evaluate the "SkipMyName" class. And if it is not designed for DI - I get an exception from DryIoc (let's say the class has two public constructors). By design - I do not need it to be registered in a DI.

So is there a way to tell DryIoc to exclude a type from being registered?


Solution

  • Filter the implementation types, not the derived service types:

    var implTypes = typeof(DemoClass)
        .GetAssembly()
        .GetLoadedTypes()
        .Where(t = t.Name != "SkipMyName");
    
    registrator.RegisterMany(implTypes);