Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8715149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:46:46+00:00 2026-06-13T05:46:46+00:00

Is it possible to register a type for all it’s implementing interfaces? E.g, I

  • 0

Is it possible to register a type for all it’s implementing interfaces? E.g, I have a:

public class Bow : IWeapon
{

    #region IWeapon Members

    public string Attack()
    {
        return "Shooted with a bow";
    }

    #endregion
}

public class HumanFighter 

{
    private readonly IWeapon weapon = null;
    public HumanFighter(IWeapon weapon)
    {
        this.weapon = weapon;
    }

    public string Fight()
    {

        return this.weapon.Attack();
    }

}

    [Test]
    public void Test2b()
    {
        Container container = new Container();
        container.RegisterSingle<Bow>();
        container.RegisterSingle<HumanFighter>();

        // this would match the IWeapon to the Bow, as it
        // is implemented by Bow
        var humanFighter1 = container.GetInstance<HumanFighter>(); 

        string s = humanFighter1.Fight();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:46:47+00:00Added an answer on June 13, 2026 at 5:46 am

    It completely depends on your needs, but typically you need to use the Container‘s non-generic registration method. You can define your own LINQ queries to query the application’s metadata to get the proper types, and register them using the non-generic registration methods. Here’s an example:

    var weaponsAssembly = typeof(Bow).Assembly;
    
    var registrations =
        from type in weaponsAssembly.GetExportedTypes()
        where type.Namespace.Contains(".Weapons")
        from service in type.GetInterfaces()
        select new { Service = service, Implementation = type };
    
    foreach (var reg in registrations)
    {
        container.Register(reg.Service, reg.Implementation);
    }
    

    If you need to batch-register a set of implementations, based on a shared generic interface, you can use the RegisterManyForOpenGeneric extension method:

    // include the SimpleInjector.Extensions namespace.
    
    container.RegisterManyForOpenGeneric(typeof(IValidator<>),
        typeof(IValidator<>).Assembly);
    

    This will look for all (non-generic) public types in the supplied assembly that implement IValidator<T> and registers each of them by their closed-generic implementation. If an type implements multiple closed-generic versions of IValidator<T>, all versions will be registered. Take a look at the following example:

    interface IValidator<T> { }
    class MultiVal1 : IValidator<Customer>, IValidator<Order> { }
    class MultiVal2 : IValidator<User>, IValidator<Employee> { }
    
    container.RegisterManyForOpenGeneric(typeof(IValidator<>),
        typeof(IValidator<>).Assembly);
    

    Assuming the given interface and class definitions, the shown RegisterManyForOpenGeneric registration is equivalent to the following manual registration:

    container.Register<IValidator<Customer>, MultiVal1>();
    container.Register<IValidator<Order>, MultiVal1>();
    container.Register<IValidator<User>, MultiVal2>();
    container.Register<IValidator<Employee>, MultiVal2>();
    

    It would also be easy to add convenient extension methods. Take for instance the following extension method that allows you to register a single implementation by all its implemented interfaces:

    public static void RegisterAsImplementedInterfaces<TImpl>(
        this Container container)
    {
        foreach (var service in typeof(TImpl).GetInterfaces())
        {
            container.Register(service, typeof(TImpl));
        }
    }
    

    It can be used as follows:

    container.RegisterAsImplementedInterfaces<Sword>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have model: [Validator(typeof(RegisterValidator))] public class RegisterModel { public string Name { get; set;
Is possible create (register) a new class in runtime using delphi. I have a
I want to know if it's possible to register a type library without using
Possible Duplicate: Register the Android App with C2DM so, I've been looking all over
A Castle Windsor question: Is it possible to register a class that has an
I am wondering if it is possible to get all bindings that have a
Possible Duplicate: Why does ASP.NET auto-generated .designer code have the incorrect type? On my
Is it possible to register ONE type for TWO interface in StructureMap ? Look:
It's possible to register a class with a parameter expected to be passed from
I want to register all my types implementing IManager so that they can be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.