Search code examples
c#reflectionstaticactivator

Clone of Static Class


I am using static list to keep some string values, when application starts just one static list is enough, but after a while i need to keep different strings in different static lists, is there any way to deep copy or clone of a static class with different name at the runtime in C#

public class Foo
{
    public static List<string> orders;
}

as you can see above, I can add and remove static Foo.orders easily, but in runtime i need another class for example Foo2 class. By the way, list must be static, however i need to reach list from different classes, also a cannot create new classes while developing, because how many static list I do not know

thank you


Solution

  • You can't create new static classes at run-time, but you can keep a static dictionary of lists:

    public class Foo
    {
         public static Dictionary<string,List<string>> Lists 
                                                          = new Dictionary<string,List<string>>
         {
             {"orders", new List<string>()}
         };
    }