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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:58:28+00:00 2026-05-27T13:58:28+00:00

I have a class which takes an IEnumerable constructor parameter which I want to

  • 0

I have a class which takes an IEnumerable constructor parameter which I want to resolve with Unity and inject an array of objects. These simple classes illustrate the problem.

public interface IThing
{
    int Value { get; }
}

public class SimpleThing : IThing
{
    public SimpleThing()
    {
        this.Value = 1;
    }

    public int Value { get; private set; }
}

public class CompositeThing : IThing
{
    public CompositeThing(IEnumerable<IThing> otherThings)
    {
        this.Value = otherThings.Count();
    }

    public int Value { get; private set; }
}

Say I want to inject four SimpleThing in to CompositeThing. I’ve tried several variations on the following Unity configuration.

<alias alias="IThing" type="TestConsoleApplication.IThing, TestConsoleApplication" />
<alias alias="SimpleThing" type="TestConsoleApplication.SimpleThing, TestConsoleApplication" />
<alias alias="CompositeThing" type="TestConsoleApplication.CompositeThing, TestConsoleApplication" />

<container>

  <register type="IThing" mapTo="SimpleThing" name="SimpleThing" />
  <register type="IThing" mapTo="CompositeThing" name="CompositeThing">
    <constructor>
      <param name="otherThings">
        <array>
          <dependency type="SimpleThing"/>
          <dependency type="SimpleThing"/>
          <dependency type="SimpleThing"/>
          <dependency type="SimpleThing"/>
        </array>
      </param>
    </constructor>
  </register>

</container>

However I get the error message The configuration is set to inject an array, but the type IEnumerable`1 is not an array type. If I change the constructor parameter to be IThing[] it works, but I don’t want to do that. What do I need to do to my Unity configuration to get this to work?

  • 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-27T13:58:29+00:00Added an answer on May 27, 2026 at 1:58 pm

    Here’s one solution that I’ve found, but it’s a bit naff. You need a wrapper class to help Unity recognise that an array is IEnumerable.

    public class ArrayWrapper<T> : IEnumerable<T>
    {
        public ArrayWrapper(T[] things)
        {
            this.things = things;
        }
    
        private IEnumerable<T> things;
    
        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            return this.things.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.things.GetEnumerator();
        }
    }
    

    You can then configure Unity to inject one of these, using Ethan’s EnumberableThing idea.

    <alias alias="IThing" type="TestConsoleApplication.IThing, TestConsoleApplication" />
    <alias alias="SimpleThing" type="TestConsoleApplication.SimpleThing, TestConsoleApplication" />
    <alias alias="CompositeThing" type="TestConsoleApplication.CompositeThing, TestConsoleApplication" />
    <alias alias="EnumerableThing" type="System.Collections.Generic.IEnumerable`1[[TestConsoleApplication.IThing, TestConsoleApplication]], mscorlib"/>
    <alias alias="ThingArrayWrapper" type="TestConsoleApplication.ArrayWrapper`1[[TestConsoleApplication.IThing, TestConsoleApplication]], TestConsoleApplication"/>
    
    <container>
      <register type="EnumerableThing" mapTo="ThingArrayWrapper">
        <constructor>
          <param name="things">
            <array>
              <dependency type="SimpleThing"/>
              <dependency type="SimpleThing"/>
              <dependency type="SimpleThing"/>
              <dependency type="SimpleThing"/>
            </array>
          </param>
        </constructor>
      </register>
    
      <register type="IThing" mapTo="SimpleThing" name="SimpleThing" />
    
      <register type="IThing" mapTo="CompositeThing" name="CompositeThing">
        <constructor>
          <param name="otherThings" dependencyType="EnumerableThing" />
        </constructor>
      </register>
    </container>
    

    Like I say though, a bit naff and awkward when you want another CompositeThing with three, five, six things etc.

    Surely Unity should be able to recognise that an array is IEnumberable and inject it?

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

Sidebar

Related Questions

I have a class which constructor takes a Jakarta enums . I'm trying to
I have a class A which takes a reference of B in its constructor.
I have a repository Class which takes in a ObjectContext called TestDB. I when
I have a class in C++ which takes an std::ostream as an argument in
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
Ok guys I basically have a class which takes in 3 strings through the
I have a case class which takes a list of functions: case class A(q:Double,
I have a generic class which takes two type parameters, Generic<A, B> . This
I have a class 'Job' which takes 3 interfaces as paremters for depedency injection.
I have a class which takes an SQL query, executes it, then binds every

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.