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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:22:25+00:00 2026-05-16T15:22:25+00:00

In Ninject there’s automatic implicit self-binding for concrete types. So without further configuration I

  • 0

In Ninject there’s automatic implicit self-binding for concrete types. So without further configuration I can resolve every type in my application, like:

Foo foo = Kernel.Get(typeof(Foo));

Now if I need an Array of Foo, how would I do that?

Foo[] foos = Kernel.Get(typeof(Foo[])); // does not work

EDIT:

For clarification, here is, what I’m actually trying to achieve: In an ASP.NET MVC application I have an AutoMapViewResult (like Jimmy Bogard is talking about in this great(!) video: http://www.viddler.com/explore/mvcconf/videos/1/ or in ASP.NET MVC 2 in Action). The difference is, that I need to inject some service to the constructor of my view model before mapping the entity to it with AutoMapper. If the source type is an array I also must instantiate an array of the view model.

So here finally some (simplified) code 🙂

public class EventsEditModel
{
    // some properties here
    public Location[] Locations { get; set; }
    public EventsEditModel(ILocationService locationService)
    {
        Locations = locationService.GetAll().ToArray();
    }
}
public class EventsListModel
{
    // some properties here
}

and here my AutoMapViewResult:

public class AutoMapViewResult : ViewResult
{
    public AutoMapViewResult(object model, Type sourceType, Type destinationType)
    {
        if (model != null)
        {
            var viewModel = IoC.Resolve(destinationType);
            ViewData.Model = Mapper.Map(model, viewModel, sourceType, destinationType);
        }
    }
}

This works great with EventsEditModel. My Index view requires an EventsListModel[], so I need to instantiate an array, which throws a System.ArgumentNullException: Value cannot be null. Parameter name: source (in the line var viewModel = IoC...)

Note: IoC.Resolve(Type serviceType) is just a wrapper for Kernel.Get(Type serviceType)

  • 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-16T15:22:26+00:00Added an answer on May 16, 2026 at 3:22 pm

    Ok, finally I found some workaround for my AutoMapViewResult. It works but seems a bit like a hack to me… However, here it is:

    public class AutoMapViewResult<TSource, TDestination> : ViewResult
    {
        public AutoMapViewResult(string viewName, string masterName, object model)
        {
            ViewName = viewName;
            MasterName = masterName;
    
            if (model.GetType().IsArray)
            {
                var viewModel = new List<TDestination>();
                foreach (var item in (TSource[])model)
                {
                    viewModel.Add(Mapper.Map<TSource, TDestination>((TSource)item, IoC.Resolve<TDestination>()));
                }
                ViewData.Model = viewModel.ToArray();
            }
            else if (model.GetType().IsGenericType)
            {
                var viewModel = new List<TDestination>();
                foreach (var item in (IEnumerable<TSource>)model)
                {
                    viewModel.Add(Mapper.Map<TSource, TDestination>((TSource)item, IoC.Resolve<TDestination>()));
                }
                ViewData.Model = viewModel;
            }
            else
            {
                ViewData.Model = Mapper.Map<TSource, TDestination>((TSource)model, IoC.Resolve<TDestination>());
            }
        }
    }
    

    The generic parameters TSource and TDestination describe always sinlge object, not arrays. So to map an array, this is the code to use it:

    public ActionResult Index()
    {
        var list = orderRepository.GetAll();
        return new AutoMapViewResult<Order, OrdersListModel>(null, null, list)
    }
    

    The AutoMapViewResult then checks, if the model, that is passed to the constructor is an array or a generic list and builds that up.

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

Sidebar

Related Questions

With the Ninject Dependency Injection Container, I can perform self-binding in a manner similar
Is there a way to use Property Injection in Ninject 2 without using the
Is there a way that I can create my own OptionalAttribute for Ninject? I
In Castle Windsor there is a feature called forwarded types where you can have
I see there is an extension for Ninject integration with asp.net-mvc but it looks
Can you use Ninject 2.0 with VS2010 RC1?
I am new to ninject, I am wondering how I can run custom initizlisation
Is anyone out there using the Prism framework with Ninject instead of Unity? I
We have a concrete singleton service which implements Ninject.IInitializable and 2 interfaces. Problem is
Question: Is there a way to use Ninject to inject dependencies into my MVC

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.