把字符串转化为类型

问题:可以得到类型的String格式的名称,想要转化为相应的类型?

ps:今天定义了好多个枚举类型,把枚举名称存放在一个ComboBox类名,控件值改变的时候要查询出这个枚举的所有属性集合,刚开始想到反射,由于效率和复杂程度的问题,想出了最佳解决方案,代码如下(此方法适用于所有类似需求):

#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

关注下面二维码,订阅更多精彩内容。
微信打赏
关注公众号(加好友):

原文地址:https://www.cnblogs.com/vipstone/p/2092499.html