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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:59:26+00:00 2026-05-23T02:59:26+00:00

I have two example classes class ClassToResolve { private List<CollectionItem> _coll; public ClassToResolve(List<CollectionItem> coll)

  • 0

I have two example classes

class ClassToResolve
{
    private List<CollectionItem> _coll;

    public ClassToResolve(List<CollectionItem> coll)
    {
        _coll = coll;
    }
}

class CollectionItem
{
    //...
}

and I need to resolve ClassToResolve

var classToResolve = new ClassToResolve(
            new List<CollectionItem>()
                {
                    new CollectionItem(),
                    new CollectionItem(),
                    new CollectionItem()
                }

            );

Now I resolve it in a way

var classToResolve = new ClassToResolve(
            new List<CollectionItem>()
                {
                    unity.Resolve<CollectionItem>(),
                    unity.Resolve<CollectionItem>(),
                    unity.Resolve<CollectionItem>()
                }
            );

Is there a way to resolve ClassToResolve with Unity using dynamic registration?

  • 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-23T02:59:27+00:00Added an answer on May 23, 2026 at 2:59 am

    Unity will understand that T[] (array) dependencies are a list of things (but not IEnumerable<T>, nor List<T>). When you change the constructor of ClassToResolve to take an CollectionItem[] instead of a List<CollectionItem> you can configure your CollectionItem types as follows:

    container.RegisterType<CollectionItem, CollectionItemA>("a");
    container.RegisterType<CollectionItem, CollectionItemB>("b");
    container.RegisterType<CollectionItem, CollectionItemC>("c");
    container.RegisterType<CollectionItem, CollectionItemD>("d");
    

    The trick here is to name all the registrations. A default (nameless) registration will never be part of a sequence dependency in Unity.

    Now you can resolve ClassToResolve without the need to register it:

    container.Resolve<ClassToResolve>();
    

    If you rather inject List<CollectionItem> you can add the following registration:

    container.RegisterType<IList<CollectionItem>, CollectionItem[]>();
    

    And for IEnumerable<CollectionItem> you can add the following line:

    container
      .RegisterType<IEnumerable<CollectionItem>, CollectionItem[]>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two List's which I want to check for corresponding numbers. for example
I have two classes. The base class is A. The inherited class is B.
I have two classes. One is a management class, which stores a bunch of
for example, we have two classes for parsing the resumes, one to parse Excel
I have two arrays of animals (for example). $array = array( array( 'id' =>
Example: I have two shared objects (same should apply to .dlls). The first shared
Simplified for example, I have two tables, groups and items . items ( id,
I have two websites, let's say they're example.com and anotherexample.net . On anotherexample.net/page.html ,
For example if I have an Enum with two cases, does it make take
I have two identical tables and need to copy rows from table to another.

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.