且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C# 泛型 无法将类型xx隐式转换为“T”

更新时间:2022-10-03 08:14:01

原文:C# 泛型 无法将类型xx隐式转换为“T”

直接奖泛型转为T是不能转换的 要先转Object

例: 

 public static T GetValue<T>(string inValue)
        {
            if (typeof(T) == typeof(Bitmap))
            {
                return (T)(Object)new Bitmap(inValue);
            }
            else
            {
              //一般类型
                return (T)Convert.ChangeType(inValue, typeof(T));
            }
            throw new Exception("");
        }