通过反射得到一个内在的 class (Windows 8.1)
Get an inner class via reflection (Windows 8.1)
我有一个叫 Languages
的 class。它包含其他几个静态 classes。例如:
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";
}
}
}
每个 class 包含一些静态字符串。我想通过反射(以及我拥有的系统语言字符串)访问这些 classes。我可以这样访问 Languages
class:
Type tt = Type.GetType("TryReflection.Languages", true);
然后我想做类似的事情:
tt.GetNestedType();
不幸的是,Windows 8.1 似乎没有 GetNestedTypes
方法。那么,我怎样才能访问其中一个 classes?谢谢。
@D Stanley 是正确的。本地化应该以不同的方式处理。
但是,要回答您的问题,请查看 PropertyDescriptor class。它允许您获得 class 的 属性 的抽象。我用它来遍历集合以使用属性和值构建数据表。
//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 );
}
我有一个叫 Languages
的 class。它包含其他几个静态 classes。例如:
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";
}
}
}
每个 class 包含一些静态字符串。我想通过反射(以及我拥有的系统语言字符串)访问这些 classes。我可以这样访问 Languages
class:
Type tt = Type.GetType("TryReflection.Languages", true);
然后我想做类似的事情:
tt.GetNestedType();
不幸的是,Windows 8.1 似乎没有 GetNestedTypes
方法。那么,我怎样才能访问其中一个 classes?谢谢。
@D Stanley 是正确的。本地化应该以不同的方式处理。
但是,要回答您的问题,请查看 PropertyDescriptor class。它允许您获得 class 的 属性 的抽象。我用它来遍历集合以使用属性和值构建数据表。
//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 );
}