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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:54:37+00:00 2026-06-12T02:54:37+00:00

Given an enum type: public enum Work { Normal, Extended } What I would

  • 0

Given an enum type:

public enum Work
{
    Normal,
    Extended
}

What I would like to do is the following.

public abstract class Builder<T>
{
    public static Builder<T> GetBuilder<T> (T work)
    {
        return new Builder<T> ();
    }
}

public class BuilderNormal : Builder<Work.Normal>
{
}

public class BuilderExtended : Builder<Work.Extended>
{
}

I specifically want to avoid using a switch/case in Builder or using a mapping that I would need to maintain when I would add a new enum value to Work, i.e. I could do this

public abstract class Builder
{
    public static Builder GetBuilder (Work work)
    {
        switch (work)
        {
            case Work.Normal:
                return new BuilderNormal ();

            case Work.Extended:
                return new BuilderExtended ();

            default:
                throw new ...
        }
    }
}

So, basically, I want to create an instance of a class depending on an enum value and the class must be a child class of an abstract class.

  • 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-06-12T02:54:38+00:00Added an answer on June 12, 2026 at 2:54 am

    You can’t in the way you’ve designed, basically. Generic type parameters are always for types, not values.

    What you can certainly do is maintain a single Dictionary<Work, Func<Builder>> to allow you to basically register factories. That will avoid the switch statement, but it’s still somewhere that you could forget to add values.

    I’d rely on unit tests to avoid the problem though – write a test which checks that you can create a Builder for every value within the enum; then if you ever add a value to the enum without adding a mapping, your test will fail.

    EDIT: Another option would be to add an attribute to the enum values to say which builder type corresponds to that value. You’d then need to extract that type with reflection and instantiate it that way.

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

Sidebar

Related Questions

Given the following code: public class RMAInfo { public enum RMAStatuses { Undefined =
Given the following information Public Enum Request As Byte None = 0 Identity =
Given a class containing an enum: public class MyClass { public enum NestedEnum {
Given the following flags, [Flags] public enum Operations { add = 1, subtract =
Scala doesn't have type-safe enum s like Java has. Given a set of related
Given the following code: public enum Pet { Cat, Dog } public interface IOwner
Given the following code, [DataContract] public class TestClass { [DataMember] public object _TestVariable; public
Given a class hierarchy where the base class defines a recursive self-type: abstract class
In Abc.hpp file the following information is defined: class Abc: public A { enum
Given the following enum: public enum Operations_PerHourType : byte { Holes = 1, Pieces

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.