mardi 17 janvier 2017

Use reflection to get a dictionary from subclasses

If I have a class of classes of static strings, like this:

public class Urls {
    public static class App1 {
        public static string URL1 = "http://....1";
        public static string URL2 = "http://....2";
    }
    public static class App2 {
        public static string URL3 = "http://....3";
        public static string URL4 = "http://....4";
    }
    private static Dictionary<string, string> _dict;
    private static Dictionary<string, string> URLDictionary {
        get {
          if(_dict != null) return _dict;
          return _dict = [WHAT GOES HERE?];
        }
    }
}

I need to use reflection cause I dont want to have to manage a dictionary, I just want to get the fields out of the classes. What do I put in [WHAT GOES HERE?] in order to made URLDictionary equal to this:

Dictionary<string, string>(){
    {"Url1", "http://.....1"},
    {"Url2", "http://.....2"},
    {"Url3", "http://.....3"},
    {"Url4", "http://.....4"}
};

? Or, even better, this:

Dictionary<string, Dictionary<string, string>>(){
    {"App1", new Dictionary<string, string>() { 
        {"Url1", "http://.....1"},
        {"Url2", "http://.....2"},
    },
    {"App2", new Dictionary<string, string>() { 
        {"Url3", "http://.....3"},
        {"Url4", "http://.....4"},
    },
};





Aucun commentaire:

Enregistrer un commentaire