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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:25:33+00:00 2026-05-14T07:25:33+00:00

I have a abstract base class that I have many inherited classes coming off

  • 0

I have a abstract base class that I have many inherited classes coming off of. What I would like to do is a static member takes in a string, the first class that can parse the string (only one of the inherited classes should be able to) and return a instance of the inherited class.

This is what i am currently doing.

public static Epl2Command GenerateCommandFromText(string command)
{
    lock (GenerateCommandFromTextSyncRoot)
    {
        if (!Init)
        {
            Assembly a = Assembly.GetAssembly(typeof(Epl2Command));
            Types = new List<Type>(a.GetTypes());
            Types = Types.FindAll(b => b.IsSubclassOf(typeof(Epl2Command)));
            Init = true;
        }
    }
    Epl2Command ret = null;
    foreach (Type t in Types)
    {

        MethodInfo method = t.GetMethod("GenerateCommand", BindingFlags.Static | BindingFlags.Public);

        if (method != null)
            ret = (Epl2Command)method.Invoke(null, new object[] { command });
        if (ret != null)
            break;
    }
    return ret;
}

I would like it so my code would check all inherited classes without having future programmers come back and edit this function when they add more inherited classes.

Is there a way I can force a inherited class to implement their own GenerateCommand(string)?

public static abstract Epl2Command GenerateCommand(string command) is not valid c#. Or am I hammering a nail in with a shoe when I should be using a hammer; any better way of doing this class factory would be appreciated.

  • 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-14T07:25:34+00:00Added an answer on May 14, 2026 at 7:25 am

    C# does not support static interfaces, so you can’t define a static builder method like

    public interface ICommand
    {
        static ICommand CreateCommand(string command);
    }
    

    I agree with Kevin that you need a Factory pattern. I’ll go a step further and say that you need a builder per command type, too. Like this

    public interface ICommandBuilder
    {
        bool CanParse(string input);
        ICommand Build(string input);
    }
    
    public interface ICommandBuilder<TCommand> : ICommandBuilder 
        where TCommand : ICommand
    {
        TCommand Build(string input);
    }
    

    Then your factory can accept any input command string, query all builders if they can parse that string, and run Build on the one that can.

    public interface ICommandFactory
    {
        ICommand Build(string input);
    }
    
    public class CommandFactory
    {
        public ICommand Build(string input)
        {
            var builder = container.ResolveAll(typeof(ICommandBuilder))
                .First(x => x.CanParse(input));
            return builder.Build(input);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have an abstract base class that many classes extend. I'd like all these
I have an abstract immutable base class that defines enforces child classes to be
I have a base class DockedToolWindow : Form, and many classes that derive from
I have an abstract base class from which many classes are derived. I want
Let's say I have an abstract base class that looks like this: class StellarObject(BaseModel):
I have two case classes that inherit from an abstract base class. I want
I have many classes inherited from the abstract base SysLaserBase. I add every laser
I have a abstract base C# class with a couple of methods that has
I have a abstract base class A and a set of 10 derived classes.

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.