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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:47:15+00:00 2026-06-15T10:47:15+00:00

Can I register initializers for Generic types with SimpleInjector at runtime? (see the last

  • 0

Can I register initializers for Generic types with SimpleInjector at runtime? (see the last line of code below)

public class BootStrapper
{
    Type baseTypeGeneric = typeof(Sample<>);
    public void BootStrap(Container container)
    {
        var types =
            from type in Assembly.GetExecutingAssembly().GetTypes()
            where type.Namespace == "NS.xyz" 
            select type;

        foreach (Type type in types)
        {
            Type baseType = baseTypeGeneric.MakeGenericType(type);

            if (type.BaseType == baseType)
            {
                container.Register(type);
            }

            //how do I get this line to work?
            container.RegisterInitializer<Sample<[type]>>(
                x => container.InjectProperties(x));
        }
    }
}
  • 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-06-15T10:47:15+00:00Added an answer on June 15, 2026 at 10:47 am

    The solution as actually rather simple: implement a non-generic interface on Sample<T>. This allows you to just register a single delegate for all sample types:

    public interface ISample { }
    
    public abstract class Sample<T> : ISample { }
    
    public void Bootstrapper(Container container)
    {
        container.RegisterInitializer<ISample>(
            instance => container.InjectProperties(instance));
    }
    

    Since RegisterInitializer will be applied for all types that implement ISample, you just need one registration.

    But to be honest, you should be very careful with applying property injection since this leads to container configuration that is hard to verify. When injecting dependencies into constructors, the container will throw an exception when a dependency is missing. With property injection on the other hand, a missing dependency is simply skipped and no exception is thrown. For this reason the InjectProperties method was deprecated in v2.6. Please review your strategy. Perhaps you should reconsider. If you are in doubt and like some feedback on your design; post a new question here at Stackoverflow.

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

Sidebar

Related Questions

I've set hibernate.generate_statistics=true and now need to register the mbeans so I can see
How can I register to event and do some actions at runtime? For example
I can register my android app with C2DM successfully using a <receiver> in my
I made a form so that users can register their email address for a
Is there a catchall handler I can register for the BeginInvoke method in C#?
I've a website where users can register. I would like to prevent all special
I have a form where my users can register to my site. They fill
I have following question on adobe omniture How we can register an iPhone app
I want to make a WCF timer service where clients can register in order
I want to build a web application where users can register and have a

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.