0

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"},
    },
};
itcropper
  • 752
  • 1
  • 7
  • 21
  • Possible duplicate of [How to get all static properties and its values of a class using reflection](http://stackoverflow.com/questions/12474908/how-to-get-all-static-properties-and-its-values-of-a-class-using-reflection) combined with [Is there a way to get a list of innerclasses in C#?](http://stackoverflow.com/questions/5566428/is-there-a-way-to-get-a-list-of-innerclasses-in-c) – Eugene Podskal Jan 17 '17 at 19:55
  • Perhaps `{ nameof(App1.URL1), App1.URL1 }` etc... – haim770 Jan 17 '17 at 19:56
  • Do Urls and AppX classes change frequently? Do you want to automate this with reflection, and not hard-code the transformation? – ironstone13 Jan 17 '17 at 20:06
  • @ironstone13 they change frequently enough such that I don't want to hardcode it. – itcropper Jan 17 '17 at 20:13

2 Answers2

0
_dict = typeof(Urls).GetNestedTypes().ToDictionary(t => t.Name, t => t.GetFields(BindingFlags.Static | BindingFlags.Public).ToDictionary(p => p.Name, p => p.GetValue(null)));
Eric
  • 1,737
  • 1
  • 13
  • 17
0

There might be a more elegant answer, but this works.

public static Dictionary<string, Dictionary<string, string>> URLs
{
    get
    {
        if (_URLs != null)
            return _URLs;

        _URLs =
            typeof(HealthURLs).GetNestedTypes(BindingFlags.Static | BindingFlags.Public)
            .Select(m => new KeyValuePair<string, Dictionary<string, string>>(
                    m.Name, 
                    m.GetFields()
                        .Select(k => new KeyValuePair<string, string>(
                            k.Name, 
                            k.GetValue(null).ToString()))
                        .ToDictionary(x => x.Key, x => x.Value)))
            .ToDictionary(x => x.Key, x => x.Value);
        return _URLs;
    }
}
itcropper
  • 752
  • 1
  • 7
  • 21