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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:46:15+00:00 2026-05-19T01:46:15+00:00

I am trying to use Ninject to implement cascading injection into a class that

  • 0

I am trying to use Ninject to implement cascading injection into a class that contains an IList field. It seems that, unless I specifically specify each binding to use in the kernel.Get method, the IList property is always injected with a list of a single default object.

The following VSTest code illustrates the problem. The first test fails because the IList field contains one MyType object with Name=null. The second test passes, but I had to specifically tell Ninject what constructor arguments to use. I am using the latest build from the ninject.web.mvc project for MVC 3.

Does Ninject specifically treat IList different, or is there a better way to handle this? Note that this seems to only be a problem when using an IList. Createing a custom collection object that wraps IList works as expected in the first test.

[TestClass()]
public class NinjectTest
{
    [TestMethod()]
    public void ListTest_Fails_NameNullAndCountIncorrect()
    {
        var kernel = new Ninject.StandardKernel(new MyNinjectModule());
        var target = kernel.Get<MyModel>();
        var actual = target.GetList();

        // Fails. Returned value is set to a list of a single object equal to default(MyType) 
        Assert.AreEqual(2, actual.Count());  

        // Fails because MyType object is initialized with a null "Name" property
        Assert.AreEqual("Fred", actual.First().Name); 
    }

    [TestMethod()]
    public void ListTest_Passes_SeemsLikeUnnecessaryConfiguration()
    {
        var kernel = new Ninject.StandardKernel(new MyNinjectModule());

        var target = kernel.Get<MyModel>(new ConstructorArgument("myGenericObject", kernel.Get<IGenericObject<MyType>>(new ConstructorArgument("myList", kernel.Get<IList<MyType>>()))));

        var actual = target.GetList();
        Assert.AreEqual(2, actual.Count());  
        Assert.AreEqual("Fred", actual.First().Name); 
    }
}

public class MyNinjectModule : NinjectModule
{
    public override void Load()
    {
        Bind<IList<MyType>>().ToConstant(new List<MyType> { new MyType { Name = "Fred" }, new MyType { Name = "Bob" } });
        Bind<IGenericObject<MyType>>().To<StubObject<MyType>>();
    }
}

public class MyModel
{
    private IGenericObject<MyType> myGenericObject;

    public MyModel(IGenericObject<MyType> myGenericObject)
    {
        this.myGenericObject = myGenericObject;
    }

    public IEnumerable<MyType> GetList()
    {
        return myGenericObject.GetList();
    }
}

public interface IGenericObject<T>
{
    IList<T> GetList();
}

public class StubObject<T> : IGenericObject<T>
{
    private IList<T> _myList;

    public StubObject(IList<T> myList)
    {
        _myList = myList;
    }

    public IList<T> GetList()
    {
        return _myList;
    }
}

public class MyType
{
    public String Name { get; set; }
}
  • 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-19T01:46:16+00:00Added an answer on May 19, 2026 at 1:46 am

    lists, collections and arrays are handled slightly different. For those types ninject will inject a list or array containing an instance of all bindings for the generic type. In your case the implementation type is a class which is aoutobound by default. So the list will contain one instance of that class. If you add an interface to that class and use this one the list will be empty.

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

Sidebar

Related Questions

I am trying to properly use Ninject to inject log4net logging into my MVC3
.. but not k.i.s.s.i.n.g I am trying to use NInject with NHibernate. Through NuGet
Hi I'm trying use a datepicker on a field I have. I'm trying to
I have been trying to use Ninject today and have a couple of questions.
In trying to configure ServiceStack.net to use Ninject as its IOC, I am getting
I'm trying to use Ninject to provide my custom MembershipProvider with an instance of
I'm trying to use Ninject with a WCF service of mine. I'm using Ninject
I am trying to use the repository pattern, ninject for DI with fluent nhibernate.
I am trying to use Ninject and OpenAccess for the first time. Please help
I am trying to use Ninject 2.0 with Asp .Net 3.5 web application. Following

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.