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

  • Home
  • SEARCH
  • 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 7550433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:12:42+00:00 2026-05-30T10:12:42+00:00

We are using Unity framework for Dependency injection. I am registerring two concrete implementations

  • 0

We are using Unity framework for Dependency injection. I am registerring two concrete implementations for same class. like…

container.RegisterType<IInterface, MyFirstClass>("first");
container.RegisterType<IInterface, MySecondClass>("second");

Now there are few classes which has contructer injection for above interface..

 public class Class1
    {
       private IInterface _myClass
       public Class1 (IInterface myClass)
       {
         _myClass = myClass // // should be instance of MyFirstClass
       }
 //.. other code
    }

   public class Class2
    {
       private IInterface _myClass

       public Class2 (IInterface myClass)
       {
         _myClass = myClass // should be instance of MySecondClass
       }
 //.. other code
    }

So now we want to resolve IInterface selectivly in above two classes. Class1 and Class2.

Class1 should be injeceted with MyFirstClass and Class2 should be injected with MyFirstClass. Is this possible to achieve via Unity framework? If not which other IoC frameworks support this?

Please suggest your opinions.

  • 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-05-30T10:12:43+00:00Added an answer on May 30, 2026 at 10:12 am

    Try this

    var container = new UnityContainer();
    container.RegisterType<IInterface, MyFirstClass>("first");
    container.RegisterType<IInterface, MySecondClass>("second");
    container.RegisterType<Class1>(new InjectionConstructor(new ResolvedParameter(typeof(IInterface), "first")));
    container.RegisterType<Class2>(new InjectionConstructor(new ResolvedParameter(typeof(IInterface), "second")));
    var class1 = container.Resolve<Class1>();
    Assert.IsInstanceOfType(class1.MyClass, typeof(MyFirstClass));
    var class2 = container.Resolve<Class2>();
    Assert.IsInstanceOfType(class2.MyClass, typeof(MySecondClass));
    
    public class MyFirstClass : IInterface
    {
    }
    
    public class MySecondClass : IInterface
    {
    }
    
    public interface IInterface
    {
    }
    
    public class Class1
    {
      public IInterface MyClass { get; set; }
      public Class1(IInterface myClass)
      {
        MyClass = myClass;
      }
    }
    public class Class2
    {
      public IInterface MyClass { get; set; }
      public Class2(IInterface myClass)
      {
        MyClass = myClass;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the Unity dependency injection framework. I have two classes, that each take
Thanks for reading. I'm using Unity framework to implement dependency injection in my app
In an ASP.NET MVC application when using a custom Dependency Injection container (in this
We are using StructureMap as our Dependency Injection (DI) framework while we are creating
I'm working on my 1st project using MS Unity IoC framework. If I have
I am using the MVVM Light framework as well as Unity for DI. I
is there a way using the Unity framework to pass an integer as an
I am already using Prism, especially the following areas: EventAggregator, RegionManager, Commanding, Dependency Injection
I am using Microsoft.Practices.Unity framework 1.2 and I can see the memory usage dramatically
I have started using Guice to do some dependency injection on a project, primarily

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.