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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:24:01+00:00 2026-05-18T05:24:01+00:00

I must do the following: var someType = ObjectFactory.GetNamedInstance(myNamedInstance); where someType can be any

  • 0

I must do the following:

var someType = ObjectFactory.GetNamedInstance("myNamedInstance");

where someType can be any implementation of ICommand.

so I registered some of them:

For(typeof(ICommand<>)).Use(typeof(Command1)).Named("myNamedInstance1");
For(typeof(ICommand<>)).Use(typeof(Command2)).Named("myNamedInstance2");

is there a way to do this in StructureMap, because GetNamedInstance requires the type parameter which I don3t know until runtime.?

  • 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-18T05:24:01+00:00Added an answer on May 18, 2026 at 5:24 am

    (For this, I’m assuming you created ICommand<T>)

    This doesn’t actually make any sense… think of the line:

    //where aString is either myNamedInstance1 or myNamedInstance2
    var someType = ObjectFactory.GetNamedInstance(aString);
    

    Now assume that you are not going to use var, and instead the actual type. What would you put there that could compile? That is, what type can someType possibly be other than object?

    Also, remember that ICommand<string> and ICommand<int> are both constructed types from ICommand<T>, but are not otherwise related – they have no common base type other than object.

    If you don’t the type until runtime, generics are not going to help a bunch – instead make your ICommand<T> inherit from some common interface – like ICommandBase – that has the methods you actually need.

    However, if you just don’t know the type in that method, you can push the unknown “up” in the compile by making the method containing that generic:

    public void Execute<T>( string commandName)
    {        
        var someType = ObjectFactory.GetNamedInstance<ICommand<T>>(commandName);
        someType.Execute();
    }
    

    Now the caller of execute needs the type param… but again you could push that up. Notice that eventually you’ll need the type parameter.

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

Sidebar

Related Questions

Supposing I have the following LambdaExpression: var itemParam = Expression.Parameter(typeof(Thing), thing); var someValue =
I have an application that must be able to do the following: var script1
According to MSDN , a hash function must have the following properties: If two
I have the following problem that other people must have encountered. I am working
I am getting the following exception: Nullable object must have a value Everything was
I have the following PHONY target in Makefile install: echo /usr/bin/shelldecrypt must be writable
I am trying to do the following but I think I must be missing
I'm creating a Rails 3.1 app and I've the following big issue: I must
I'm trying to do the following: using (var tx = sqlConnection.BeginTransaction()) { var command
I have the following in javascript: var entriesString = ''; $$('select[id=shopId]').each(function(elem, i){ shops[i] =

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.