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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:02:31+00:00 2026-06-03T00:02:31+00:00

I frequently need a global hard-coded mapping between an enum and another object (a

  • 0

I frequently need a global hard-coded mapping between an enum and another object (a string in this example). I want to co-locate the enum and mapping definitions to clarify maintenance.

As you can see, in this example, an annoying class with one static field is created.

public enum EmailTemplates
{
    // Remember to edit the corresponding mapping singleton!
    WelcomeEmail,
    ConfirmEmail
}

public class KnownTemplates
{
    public static Dictionary<EmailTemplates, string> KnownTemplates;
    static KnownTemplates() {
        KnownTemplates.Add(EmailTemplates.WelcomeEmail, "File1.htm");
        KnownTemplates.Add(EmailTemplates.ConfirmEmail, "File2.htm");
    }
}

Sometimes the mapping class can have more function and a meaningful name, and the mapping activity can even be private. But that only pollutes the maintenance/correlation problem.

Anyone have a good pattern for this?

  • 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-03T00:02:33+00:00Added an answer on June 3, 2026 at 12:02 am

    You can use attributes to annotate the enumeration and then use reflection to build the dictionary.

    [AttributeUsage(AttributeTargets.Field)]
    sealed class TemplateAttribute : Attribute {
    
      public TemplateAttribute(String fileName) {
        FileName = fileName;
      }
    
      public String FileName { get; set; }
    
    }
    
    enum EmailTemplate {
    
      [Template("File1.htm")]
      WelcomeEmail,
    
      [Template("File2.htm")]
      ConfirmEmail
    
    }
    
    class KnownTemplates {
    
      static Dictionary<EmailTemplate, String> knownTemplates;
    
      static KnownTemplates() {
        knownTemplates = typeof(EmailTemplates)
          .GetFields(BindingFlags.Static | BindingFlags.Public)
          .Where(fieldInfo => Attribute.IsDefined(fieldInfo, typeof(TemplateAttribute)))
          .Select(
            fieldInfo => new {
              Value = (EmailTemplate) fieldInfo.GetValue(null),
              Template = (TemplateAttribute) Attribute
                .GetCustomAttribute(fieldInfo, typeof(TemplateAttribute))
            }
          )
          .ToDictionary(x => x.Value, x => x.Template.FileName);
      }
    
    }
    

    If you do this a lot you can create a more general generic function that combines enumeration values with an attribute associated with that enumeration value:

    static IEnumerable<Tuple<TEnum, TAttribute>> GetEnumAttributes<TEnum, TAttribute>()
      where TEnum : struct
      where TAttribute : Attribute {
      return typeof(TEnum)
        .GetFields(BindingFlags.Static | BindingFlags.Public)
        .Where(fieldInfo => Attribute.IsDefined(fieldInfo, typeof(TAttribute)))
        .Select(
          fieldInfo => Tuple.Create(
            (TEnum) fieldInfo.GetValue(null),
            (TAttribute) Attribute.GetCustomAttribute(fieldInfo, typeof(TAttribute))
          )
        );
    }
    

    And use it like this:

    knownTemplates = GetEnumAttributes<EmailTemplate, TemplateAttribute>()
      .ToDictionary(tuple => tuple.Item1, tuple => tuple.Item2.FileName);
    

    For even more fun you can create an extension method:

    static class EmailTemplateExtensions {
    
      static Dictionary<EmailTemplate, String> templates;
    
      static EmailTemplateExtensions() {
        templates = GetEnumAttributes<EmailTemplate, TemplateAttribute>()
          .ToDictionary(tuple => tuple.Item1, tuple => tuple.Item2.FileName);
      }
    
      public static String FileName(this EmailTemplate emailTemplate) {
        String fileName;
        if (templates.TryGetValue(emailTemplate, out fileName))
          return fileName;
        throw new ArgumentException(
          String.Format("No template defined for EmailTemplate.{0}.", emailTemplate)
        );
      }
    
    }
    

    Then calling EmailTemplate.ConfirmEmail.FileName() will return File2.htm.

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

Sidebar

Related Questions

As an optimization, I decided to put an object I frequently need - an
I need to merge between dev and master frequently. I also have a commit
I have two development branches in git and I frequently need to change between
I frequently need to have a thread wait for the result of another thread.
I frequently need to make many replacements within files. To solve this problem, I
Often I need to minimise object allocations within code that runs very frequently. Of
I frequently need to ssh into a server, but I can't ssh into it
In data processing, I frequently need to create a lookup data structure to map
I need to frequently duplicate one database on my MySQL server to a mirror
I need to do testing on clean machines frequently so I need some kind

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.