0

I have a class called Languages. It contains several other static classes. For instance:

namespace TryReflection
{
    class Languages
    {
        public static class it
        {
            public static string PLAY = "Gioca";
            public static string START = "Inizia!";
            public static string NEXT = "Prossimo";
            public static string STOP = "Ferma!";
            public static string SCORE = "Punti";
            public static string RESTART = "Per cambiare la difficoltà inizia una nova partita";
        }

        public static class en
        {
            public static string PLAY = "Play";
            public static string START = "Start!";
            public static string NEXT = "Next";
            public static string STOP = "Stop!";
            public static string SCORE = "Score";
            public static string RESTART = "To change difficulty restart your match";
        }
    }
}

Each class contains some static strings. I'd like to access these classes via reflection (and with the system language string that I have). I can access the Languages class this way:

Type tt = Type.GetType("TryReflection.Languages", true); 

And then I'd like to do something like:

tt.GetNestedType();

Unfortunately, it seems that on Windows 8.1 there's no GetNestedTypes method. So, how can I access one of these classes? Thank you.

superpuccio
  • 11,674
  • 8
  • 65
  • 93
  • 1
    If you are planning to use this to localize your app you are not doing it the standard way. Follow [Microsoft's guidelines](https://msdn.microsoft.com/en-us/library/windows/apps/hh969150.aspx) by using resource files and the built-in localization libraries. – D Stanley Sep 22 '15 at 17:18
  • 1
    which .net framework version do you use? i dont know anything about windows phone but maybe i can find you a workaround if i'd knew the version. also there are imo much much better ways to do what you are trying to do, are you interested in suggestions? – x4rf41 Sep 22 '15 at 17:35
  • Check this http://stackoverflow.com/a/5566476/1641556 – Elshan Sep 22 '15 at 17:39
  • WinRT has made reflection a bit, erm, gothic. You'll have to use TypeInfo.DeclaredNestedTypes instead. – Hans Passant Sep 22 '15 at 17:51

1 Answers1

1

@D Stanley is correct. Localization should be handled differently.

However, to answer your question, take a look at PropertyDescriptor class. It allows you to get an abstraction of a property of a class. I use it to iterate over collections to build DataTables using the properties and values.

//Get the properties as a collection from the class
Type tt = Type.GetType("TryReflection.Languages", true);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(tt);  

for (int i = 0; i < props.Count; i++)
{
    PropertyDescriptor prop = props[i];
    string propertyInfo = String.Format("{0}: {1}", 
        prop.Name, 
        prop.PropertyType.GetGenericArguments()[0]));

    Console.Out.Write( propertyInfo );
}
Ian
  • 738
  • 5
  • 13