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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:12:56+00:00 2026-05-23T09:12:56+00:00

I have many classes that implements IBuilder<> interface such the ones below UPDATED: each

  • 0

I have many classes that implements IBuilder<> interface such the ones below

UPDATED:
each Model1, Model2… inherits from IModel

public class A : IBuilder<Model1>
{
     public Model1 Create(string param)
     {
         return new Model1();
     }
}
public class B : IBuilder<Model2>
{
     public Model2 Create(string param)
     {
          return new Model2();
     }
}

I’m using StructureMap to register all classes that inherit IBuilder<>

Scan(x =>
{
       x.TheCallingAssembly();
       x.AddAllTypesOf(typeof(IViewModelBuilder<>));                
});

UPDATED

Now, every time I need to get model of some Module I call Do function

public IModel Do(Module module)
{
        //ModelSettings is taken from web.config  
        var builderType = Type.GetType(string.Format("{0}.{1}ModelBuilder,{2}", ModelSettings.Namespace, module.CodeName, ModelSettings.Assembly));
        var builder = ObjectFactory.GetInstance(t) as IViewModelBuilder<>;

        return builder.Create("");            
}

I get compilation error in the line ObjectFactory.GetInstance(t) as IViewModelBuilder<>.
Many posts suggest to create NOT generic interface(IViewModelBuilder) and let the generic one to inherit it. And then I could make the casting like

ObjectFactory.GetInstance(t) as IViewModelBuilder

Is this the only way?

Thank you

  • 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-23T09:12:56+00:00Added an answer on May 23, 2026 at 9:12 am

    Couldn’t you make Do() generic?

    var m = Do<B>();
    
    public T Do<T>()
    { 
       var builder = (IViewModelBuilder<T>)ObjectFactory.GetInstance(typeof(T));
       return builder.Create("");
    }
    

    If you can’t, using non-generic interface is probably your best bet, but there are other options like using reflection or C# 4’s dynamic:

    var m = Do(typeof(B));
    
    public object Do(Type t)
    { 
       dynamic builder = ObjectFactory.GetInstance(t);
       return builder.Create("");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen many apps that take instrument classes and take -javaagent as a
In my project I have a class that is inherited by many other classes.
I am using Visual Studio 2008 Code Coverage and I have many classes that
I have two C# classes that have many of the same properties (by name
Many classes in Cocoa/Cocoa Touch have the NS prefix. What does it mean?
I have many small files containing code fragments, pseudo-code algorithms, classes, templates, SQL-samples, etc.,
I have the following 3 classes Book Product SpecialOptions There are many Books, and
I have a few model classes with basic one-to-many relationships. For example, a book
I have many items inside a list control. I want each item to have
I have many jobs that have a step to send 1 specific email out

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.