简体   繁体   中英

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

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>()}
     };
}

If you absolutely must be using a static class to handle this and you are expecting to be manipulating it after it is instantiated (eg creating other lists, etc.) then you might consider creating another element that could handle that behavior such as a List<List<string>> or a Dictionary<string,List<string>> :

public static Dictionary<string,List<string>> Orders = new Dictionary<string, List<string>>();

If you were to use a dictionary, then you could store a key for each of your available lists (ie List1 , List2 and then access each list by using its key) :

Foo.Orders["List1"]; // yields your first list

and

Foo.Orders.Add("List2",new List<string>()); // Create a new list in your Dictionary

If you really must have the convenience of referencing the list without an instance being passed around, you'll want to move away from a static class (which is global by definition) and into the realm of Singletons which are stored in whatever scope you prefer (cached for the thread or execution path, for example).

However, I'd seriously consider rethinking your approach, as passing instances might be more appropriate from the sound of it.

Best practice is to create a separate public static field for every possible list you could ever need. I would write a script to generate the code for at least 10,000 lists.

public class Foo
{
    public static List<string> orders1;
    public static List<string> orders2;
    public static List<string> orders3;
    public static List<string> orders4;
    public static List<string> orders5;
    public static List<string> orders6;
    public static List<string> orders7;
    public static List<string> orders8;
    public static List<string> orders9;
    public static List<string> orders10;
    public static List<string> orders11;
    public static List<string> orders123;
    public static List<string> orders13;
    public static List<string> orders14;
    public static List<string> orders15;
    public static List<string> orders16;
    public static List<string> orders17;
    public static List<string> orders18;
    public static List<string> orders19;
    public static List<string> orders20;
    public static List<string> orders21;
    public static List<string> orders22;
    public static List<string> orders23;
    public static List<string> orders24;
    public static List<string> orders25;
    public static List<string> orders26;
    public static List<string> orders27;
    public static List<string> orders28;
    public static List<string> orders29;
    public static List<string> orders30;
    public static List<string> orders31;
    public static List<string> orders32;
    public static List<string> orders33;
    public static List<string> orders34;
    public static List<string> orders35;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM