问题:可以得到类型的String格式的名称,想要转化为相应的类型?
ps:今天定义了好多个枚举类型,把枚举名称存放在一个ComboBox类名,控件值改变的时候要查询出这个枚举的所有属性集合,刚开始想到反射,由于效率和复杂程度的问题,想出了最佳解决方案,代码如下(此方法适用于所有类似需求):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #region 根据类型名称(string)返回类型 /// <summary> /// 根据类型名称(string)返回类型 /// </summary> /// <param name="StringInfo">类型名称(string)</param> /// <returns>Type</returns> public Type GetTypeByStringInfo( string StringInfo) { Type type = null ; if (!String.IsNullOrEmpty(StringInfo)) { switch (StringInfo) { case "Class" : // Class自定义的类名称 type = typeof (Class); break ; default : break ; } } return type; } #endregion |
本文转自王磊的博客博客园博客,原文链接:http://www.cnblogs.com/vipstone/archive/2011/06/28/2092499.html,如需转载请自行联系原作者