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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:58:46+00:00 2026-05-29T07:58:46+00:00

I want a magic to happen like that… class SomeClass {} public static void

  • 0

I want a magic to happen like that…

class SomeClass {}

public static void main()

    MagicFunctionOrMacrosOrSomethingThatGetTypeOf("SomeClass") some_var = null;<br />
}

Maybe it’s crazy, but is it possible?
I need it to avoid crazy large switch case, because has a lot of classes, but at certain point of code is unable to tell what of the classes to create.

Let’s have an example:
I have base class:
class baseCLASS {}

have a lot of child classes:
class class1 : baseCLASS {}
class class2 : baseCLASS {}
…
class classn : baseCLASS {}

have some flag that comes from outside to events handler:
string class_name; //actually i use int flag that should have connected with string names in dictionary.

have forced to make this:

   switch(class_name)

   case "class1":
      class1 some_class = new class1();
      some_class.RunHandler();
      break;
   case "class2":
      class2 some_class = new class2();
      some_class.RunHandler();
      break;
   case "classn":
      classn some_class = new classn();
      some_class.RunHandler();
      break;
   }

I want to replace that large switch statement by something like this:

MagicFunctionOrMacrosOrSomethingThatGetTypeOf(class_name) some_var = null;
... some initialization actions with some_var
some_var.Build();
some_var.RunHandler();

oh my…

  • 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-29T07:58:47+00:00Added an answer on May 29, 2026 at 7:58 am

    You have two problems:

    1. Given user input, you want to instantiate an object of a varying class. For this, you want a mapping between user input and .NET type name. For the first: you can use a convention — ensure that the type name matches the input; or you can use a Dictionary<string,type>, and populate it; or you can attach an attribute to each class, showing which “verb” it handles. Once you’ve done that, you can use Activator.CreateInstance to (at runtime) create an instance of your unknown class. This returns object.
    2. Calling methods on the unknown object. Either define a base class or common interface for the classes (e.g. IRunnable or something), or use dynamic, which gives you duck typing.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

first I want to say that I hope this doesn't look like I am
Using tornado, I want to create a bit of middleware magic that ensures that
I want to use magic function __set() and __get() for storing SQL data inside
I want to turn off PHP's magic quotes. I don't have access to php.ini.
we've got an existing webapplication written in magic. But now we eventually want to
want to know why String behaves like value type while using ==. String s1
It is just magic that I put some file in my site's Bin folder
I have an Android application that writes data to the SD card. I want
Here's me fiddle: http://jsfiddle.net/6yU6N/10/ I want to work some CSS magic on the table
I have an application that I just would like to use in portrait mode,

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.