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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:15:01+00:00 2026-05-12T08:15:01+00:00

Let’s say I have base class FooParent , and it has a numerous amount

  • 0

Let’s say I have base class FooParent, and it has a numerous amount of FooChildren. At runtime I must create an instance of one of the FooChildren. How would I do this? I realize I could create a huge map (and use delegates) or a huge switch/case statement, but that just seems a bit sloppy. In something like PHP, I can easily create a class dynamically like this:

$className="FooClass";
$myNewFooClass=new $className; //makes a new instance of FooClass

(you can also do this using reflection).

Does .NET have anything like this? Is reflection an option, and does it have any performance penalties? If not, what other options do I have?

The type of class will be determined by a JSON request. The variable could be anything I want..it could be an integer if I wanted to do an enum, or it could be the full class name. I haven’t created it yet so I’m undecided.

  • 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-12T08:15:01+00:00Added an answer on May 12, 2026 at 8:15 am

    You can do it with reflection if you really want, but there will be performance penalties. Whether they’re significant or not will depend on your exact situation.

    Depending on exactly what you want to do, I’d quite possibly go with either a switch/case statement or a map, as you suggest. In particular, that would be useful if you need to pass different arguments to different constructors based on the type you’re constructing – doing that via reflection would be a bit of a pain in that you’d already be special-casing different types.


    EDIT: Okay, so we now know that there will always be a parameterless constructor. In that case, your JSON could easily contain the class name without the namespace (if they’re all in the same namespace) and your method could look something like this:

    public FooParent CreateFoo(string name)
    {
        if (name == null)
        {
            throw new ArgumentNullException("name");
        }
        string fullName = "Some.NameSpace." + name;
        // This is assuming that the type will be in the same assembly
        // as the call. If that's not the case, we can look at that later.
        Type type = Type.GetType(fullName);
        if (type == null)
        {
            throw new ArgumentException("No such type: " + type);
        }
        if (!typeof(FooParent).IsAssignableFrom(type))
        {
            throw new ArgumentException("Type " + type +
                                        " is not compatible with FooParent.");
        }
        return (FooParent) Activator.CreateInstance(type);
    }
    

    Where do you determine the name to use? If it’s passed in somewhere, a switch statement can be very simple when reformatted away from the norm a little:

    public FooParent CreateFoo(string name)
    {
        switch (name)
        {
            case "Foo1":      return new Foo1();
            case "Foo2":      return new Foo2();
            case "Foo3":      return new Foo3();
            case "Foo4":      return new Foo4();
            case "FooChild1": return new FooChild1();
            default:
                throw new ArgumentException("Unknown Foo class: " + name);
        }
    }
    

    Mind you, having just written that out I’m not sure it has any real benefit (other than performance) over using Type.GetType(name) and then Activator.CreateInstance(type).

    How does the caller know the class name to pass in? Will that definitely be dynamic? Is there any chance you could use generics? The more you could tell us about the situation, the more helpful we could be.

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

Sidebar

Ask A Question

Stats

  • Questions 204k
  • Answers 204k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Havent tried that, but have used tortoise for viewing history… May 12, 2026 at 8:41 pm
  • Editorial Team
    Editorial Team added an answer Your code is using two different contexts. You check one,… May 12, 2026 at 8:41 pm
  • Editorial Team
    Editorial Team added an answer You use a string function to verify the presence of… May 12, 2026 at 8:41 pm

Related Questions

I have a French site that I want to parse, but am running into
Let's say you create a wizard in an HTML form. One button goes back,
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let me try to explain what I need. I have a server that is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.