Search code examples
.netunity-containerauto-registration

Unity.AutoRegistration - registers only interfaces and types with the same name


Unity.AutoRegistration works fine when a type name matches an interface name, for example, TClass and ITClass. I want interface names go without prefix "T", like IClass and need that IClass to match TClass.

But when I rename the interface, the auto-registration fails. Do I have to match the type/interface names, or is there a way to accomplish my need?


Solution

  • After consulting with the author, here is the required code:

    UContainer
        .ConfigureAutoRegistration()
        .LoadAssemblyFrom(Assembly.GetEntryAssembly().Location)
        .ExcludeSystemAssemblies()
        .Include(If.ImplementsITypeName, Then.Register())
        .Include(
            type => type.GetInterfaces().Any(i => i.Name.StartsWith("I") && i.Name.Substring(1) == type.Name.Substring(1)), Then.Register())
        .Include(If.ImplementsSingleInterface, Then.Register())
        .ApplyAutoRegistration();