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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:41:45+00:00 2026-05-25T11:41:45+00:00

I am not sure the best approach to this problem, be it through reflection,

  • 0

I am not sure the best approach to this problem, be it through reflection, redesigning my classes altogether, or doing something simple.

Basically I have a base class, and I can have any number of subclasses which inherit from it. Let’s call the base class Shape and the subclasses CircleShape, RectangleShape, etc.

The base class is never itself instantiated, only the subclasses. Some are never instatiated, some are instantiated many times throughout the life of the program.

Sometimes I need information specific to a subclass before I instantiate it. Right now I use an enum to differentiate all subclass types. And I instantiate each subclass based on the enum in a switch statement, like this:

switch (shapeType)
{
case CircleShape:
    shape = new CircleShape();

case SquareShape:
    shape = new RectangleShape();
}

But say instead of having to use this kind of hardcoded switch statement, I wanted to enumerate through all the subclasses. Is there a way to automatically retrieve a list of subclasses and access their STATIC members for info about them (before instantiating them)? Or is it easier to manually instantiate each class once and add them to an array so an I enumerate through them (but not tie those instances to any actual data).

Or should I do something completely different?

  • 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-25T11:41:45+00:00Added an answer on May 25, 2026 at 11:41 am

    You could use reflection to enumerate all your classes, but this is not a very efficient way to do things since it is kind of slow.

    If they are all in the same assembly you could do something like:

       class Shape
       {
          /* ... */
       }
    
       class CircleShape : Shape
       {
          public static string Name
          {
             get
             {
                return "Circle";
             }
          }
       }
    
       class RectangleShape : Shape
       {
          public static string Name 
          {
             get
             {
                return "Rectangle";
             }
          }
       }
    
       class Program
       {
          static void Main(string[] args)
          {
             var subclasses = Assembly.GetExecutingAssembly().GetTypes().Where(type => type.IsSubclassOf(typeof(Shape)));
             foreach (var subclass in subclasses)
             {
                var nameProperty = subclass.GetProperty("Name", BindingFlags.Public | BindingFlags.Static);
                if (nameProperty != null)
                {
                   Console.WriteLine("Type {0} has name {1}.", subclass.Name, nameProperty.GetValue(null, null));
                }
             }
          }
       }
    

    Of course you could also use attributes instead of static members which would probably preferable if you want to decorate the classes with information that you wanted to look up at runtime. There are many examples of how attributes work around the internet.

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

Sidebar

Related Questions

I'm not entirely sure what is the best approach to this problem, so hopefully
So I'm not sure what the best way to accomplish this is, but basically
I am thinking on the following approach but not sure if its the best
I've got a problem I'm not sure how best to solve. I have an
I find this comes up a lot, and I'm not sure the best way
I'm not sure what the best api for simple 2d graphics with Java is.
I'm not sure how to best explain this, so this may be a bit
Ok so this is alittle bit obscure and I'm not sure its the best
I am using asp.net mvc and I am not sure how to approach this
I am not sure where the best place to put validation (using the Enterprise

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.