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 3439692
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:20:28+00:00 2026-05-18T08:20:28+00:00

Is it possible to make unity try all defined constructors starting with the one

  • 0

Is it possible to make unity try all defined constructors starting with the one with most arguments down to the least specific one (the default constructor)?

Edit

What I mean:

foreach (var constructor in concrete.GetConstructorsOrderByParameterCount())
{
   if(CanFulfilDependencies(constructor))
   {
       UseConstructor(constructor);
       break;
   }
}

I don’t want Unity to only try the constructor with most parameters. I want it to continue trying until it finds a suitable constructor. If Unity doesn’t provide this behavior by default, is it possible to create an extension or something to be able to do this?

Edit 2

I got a class with two constructors:

public class MyConcrete : ISomeInterface
{
    public MyConcrete (IDepend1 dep, IDepend2 dep2)
    {}

    public MyConcrete(IDepend1 dep)
    {}
}

The class exists in a library which is used by multiple projects. In this project I want to use second constructor. But Unity stops since it can’t fulfill the dependencies by the first constructor. And I do not want to change the class since the first constructor is used by DI in other projects.

Hence the need for Unity to try resolving all constructors.

  • 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-18T08:20:28+00:00Added an answer on May 18, 2026 at 8:20 am

    Unity will choose the constructor with the most parameters unless you explicitly tag a constructor with the [InjectionConstructor] attribute which would then define the constructor for Unity to use.

    When you state a suitable constructor; that is somewhat contingent on the environment. If for instance you always want to guarantee that a certain constructor is used when making use of Unity use the attribute mentioned previously, otherwise explicitly call the constructor you want to use.

    What would be the point of Unity “trying” all constructors? It’s purpose is to provide an instance of a type in a decoupled manner. Why would it iterate through the constructors if any constructor will create an instance of the type?

    EDIT:

    You could allow the constructor with the most params to be used within the project that does not have a reference to that type within its container by making use of a child container. This will not force the use of the constructor with a single param but it will allow the constructor with 2 params to work across the projects now.

    You could also switch to using the single constructor across the board and force the other interface in via another form of DI (Property Injection), not Constructor Injection…therefore the base is applicable across the projects which would make more sense.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Q: My question consists of three parts: 1-i wanna to ask how to make
Is it possible to import the array type in Java? I know this will
Basically, here's the situation. I have the following: public IService Service { get; set;
I am currently refactoring a very useful but poorly designed class in C++, and
I have a class MyClass with the following constructor: public MyClass(IRepository repository, AnotherClass anObject)
So I have this reaaaally long method that I want to test. I could
I have a class, BillMedicine, which is many-to-many table for Bill and Medicine. The
I'm working on a web based ladder system for a game. It is very
I am trying to define the best way to refactor the project I am

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.