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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:47:08+00:00 2026-05-24T08:47:08+00:00

I want to register all my types implementing IManager so that they can be

  • 0

I want to register all my types implementing IManager so that they can be used as the type T for the generic Lazy<T> class.

For example:

public TetraRadioPropertyUpdater(Lazy<IRadioManager> lazyRadioManager)

I use a self made scanner because my concrete types and interfaces are internal and therefor I cannot use the built in StructureMap scan mechanism.

In the first statement of the loop register all my IManager types like For<IRadioManager>().Singleton().Use<RadioManager>()

As well, I want them to be registered so that they can be used as the generic type for Lazy<T> like For<Lazy<IRadioManager>().Use<Lazy<RadioManger>>()

 InterfaceScanner<IManager> interfaceScanner = new InterfaceScanner<IManager>();

 // managerMapping looks like:
 // { IRadioManager, RadioManager }
 // { ICallManager, CallManager }
 // .. more manager interface to plugin type pairs  
 foreach (KeyValuePair<Type, Type> managerMapping in interfaceScanner.Process())
 {
    // the key is the plugin type, value is the concrete type
    For(managerMapping.Key).Singleton().Use(managerMapping.Value);

    // something like this.. ?
    For(typeof(Lazy<>)).Singleton().Use(c => new Lazy(() => c.GetInstance(managerMapping.Value)));
 }

Is this possible? How do I need to configure it for StructureMap?

  • 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-24T08:47:09+00:00Added an answer on May 24, 2026 at 8:47 am

    UPDATE: StructureMap v3 implements this out of the box, so this trick is no longer necessary.


    You can explicitly register the classes like this:

    container = new Container(x =>
    {
        x.Scan(y =>
        {
            y.TheCallingAssembly();
            y.WithDefaultConventions();
        });
    
        x.For<Lazy<IFoo>>().Use(y => new Lazy<IFoo>(y.GetInstance<Foo>));
        x.For<Lazy<IBar>>().Use(y => new Lazy<IBar>(y.GetInstance<Bar>));
        x.For<Lazy<IBaz>>().Use(y => new Lazy<IBaz>(y.GetInstance<Baz>));
    });
    

    It would be nicer if this were done automatically, though. Ideally, the following syntax would be nice.

    x.For(typeof(Lazy<>)).Use(typeof(Lazy<>));
    

    Unfortunately, at runtime, StructureMap will attempt to find the “greediest” constructor for Lazy<T> and settle on public Lazy(Func<T> valueFactory, bool isThreadSafe). Since it doesn’t know what to do with the boolean isThreadSafe parameter. It will throw an exception. You can explicitly tell StructureMap what value to use like this.

    x.For(typeof(Lazy<>)).Use(typeof(Lazy<>))
     .CtorDependency<bool>("isThreadSafe").Is(true);
    

    Which will stop the exceptions, and use the value of “true” for the isThreadSafe parameter. The documentation for Lazy states that the thread safety mode of the default Lazy(Func<T> valueFactory) constructor is LazyThreadSafetyMode.ExecutionAndPublication, which is also what you get by passing true into the isThreadSafe parameter of the constructor above, so we’ll get the same behavior as if we called the constructor we actually wanted to use in the first place (e.g. Lazy(Func<T> valueFactory)). You can read about the Lazy class in more detail here.

    I realize that this doesn’t directly answer your question with regard to the custom scanner class, but it should give you a good start, I would hope.

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

Sidebar

Related Questions

I want to register to get notified of all Java changes in Eclipse. I
I want to register a specific instance of an object for a type in
I want to register a script with wordpress that will work on the backend.
Most of the examples I see for Castle Windsor auto-register type that derive from
It is my understanding that since type/class unification every value is of a type
I am trying to use Generic DataContract class so that I don't have to
In a proof-of-concept project I'm working on, I have a generic abstract class that
I want to have an object that allows other objects of a specific type
I have a class that I want to have access to my IOC container
I want to register for screen resolution changes on the Mac. I 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.