   Creating enumerations at runtime C#?
  I've created a class to create enumerations during runtime but I ran into a problem.What I wanted was a way to create enumerations from a file of strings like:rat mouse cathave rat enumerate to 1, mouse to 2, cat to 3...The way things are suppose to work is that I would inheirate a new class from CEnumerator for each set of enumerations that I wanted to create during runtime. But I found that the inheirated classes share the same protected static variables, making them effectively the same enumeration.Is there a better way to create enumerations at runtime, or is there a way that I can inheirate static variables without them being shared?struct EnumeratorNode    {    public uint ID;    public string Name;    }    public abstract class CEnumerator    {   protected static ArrayList m_Enumerations;   protected static uint m_NextID;   //return the number of enumerations   public static int Count   {  get  { return m_Enumerations.Count;  }   }   //find matching ID. return 0 if not found   public static uint NameToID(string name)   {  foreach(EnumeratorNode Temp in m_Enumerations)  { if(Temp.Name == name) {return Temp.ID; }  }  return 0;   }   //find matching string. return ".INVALID." if string not found   public static string IDToName(uint ID)   {  foreach (EnumeratorNode Temp in m_Enumerations)  { if (Temp.ID == ID)return Temp.Name;  }   return ".INVALID.";   }   public static ErrorMessage CreateFromFile(string FileName)   {  WhiteSpaceSensitiveReader fin;   string temp = "";  //Create the arraylist  m_Enumerations = new ArrayList();  try  { fin = new WhiteSpaceSensitiveReader(FileName);  }   //if file failed to open, return an error  catch(System.IO.FileNotFoundException)  { return ErrorMessage.emGeneralError;  }   while(fin.ReadString(ref temp))   { EnumeratorNode tempnode = new EnumeratorNode();   tempnode.Name = temp;   tempnode.ID = m_NextID;   m_NextID++;   //Insert at end of list   m_Enumerations.Add(tempnode);   }   return ErrorMessage.emNoError;   }    }    public class CMapEnumerator : CEnumerator    {    }    public class CCharacterEnumerator : CEnumerator    {    }
  Move access to these:   protected static ArrayList m_Enumerations;   protected static uint m_NextID;into a virtual accessor (a method that needs to be overridden by a child class).In your child classes (the specific types of enumerations) have a copy of the above static data, and override the accessors for the parent.;  Thanks for the suggestion, but unless I'm mistaken you can't have a property that is both static and virtual/abstractprotected abstract static ArrayList Enumerations   {  get;  set;   }//Generates the following errorerror CS0112: A static member 'Dungeoneer.Common.CEnumerator.Enumerations' cannot be marked as override, virtual, or abstract;  Are you going to emit a new class during runtime using reflection's type creation services?  If you are, just emit an enum instead.If you're not, and you're going to define all of your 'classes' during compile time but make a lookup table to different instances of the enumeration at runtime, then you should do this:public class RuntimeEnumManager{  static Dictionary<string,List<EnumerationNode>> enumTable = new Dictionary<string,List<EnumerationNode>>();  public static void AddNewEnum(string name, List<EnumerationNode> list)  {    enumTable[name] = list;  }  public static List<EnumerationNode> GetEnum(string name)  {    // throws if key not present - handle in caller    return enumTable[name];  }}For efficiency you could change the string key to something quicker to compare.And if you look at the code long enough you will realize that the only thing that you need is that static Dictionary<key,List<EnumerationNode>> object.(The C# version1 equivalent is Hashtable-of-string/ArrayList)   ;Quote:Original post by Procyon LotorThanks for the suggestion, but unless I'm mistaken you can't have a property that is both static and virtual/abstract*** Source Snippet Removed ***Have an interface to the property that is virtual in the parent.The child implements the virtual interface, getting it's data from a static property stored in the child class.   ;  I haven't used interfaces before. Would you mind posting an example of how that would work?If I do something like this I get an error.interface IEnumerator    {//error CS0106: The modifier 'static' is not valid for this item   static ArrayList Enumerations   {  get;  set;   }//error CS0106: The modifier 'static' is not valid for this item   static uint NextID   {  get;  set;   }    }public abstract class CEnumerator : IEnumerator    {  }public class CMapEnumerator : CEnumerator{}