C#通用类型转换 Convert.ChangeType
来源:互联网 发布:java项目开发案例 编辑:程序博客网 时间:2024/05/16 07:23
static public object ChangeType(object value, Type type)
{
if (value == null && type.IsGenericType) return Activator.CreateInstance(type);
if (value == null) return null;
if (type == value.GetType()) return value;
if (type.IsEnum)
{
if (value is string)
return Enum.Parse(type, value as string);
else
return Enum.ToObject(type, value);
}
if (!type.IsInterface && type.IsGenericType)
{
Type innerType = type.GetGenericArguments()[0];
object innerValue = ChangeType(value, innerType);
return Activator.CreateInstance(type, new object[] { innerValue });
}
if (value is string && type == typeof(Guid)) return new Guid(value as string);
if (value is string && type == typeof(Version)) return new Version(value as string);
if (!(value is IConvertible)) return value;
return Convert.ChangeType(value, type);
}
{
if (value == null && type.IsGenericType) return Activator.CreateInstance(type);
if (value == null) return null;
if (type == value.GetType()) return value;
if (type.IsEnum)
{
if (value is string)
return Enum.Parse(type, value as string);
else
return Enum.ToObject(type, value);
}
if (!type.IsInterface && type.IsGenericType)
{
Type innerType = type.GetGenericArguments()[0];
object innerValue = ChangeType(value, innerType);
return Activator.CreateInstance(type, new object[] { innerValue });
}
if (value is string && type == typeof(Guid)) return new Guid(value as string);
if (value is string && type == typeof(Version)) return new Version(value as string);
if (!(value is IConvertible)) return value;
return Convert.ChangeType(value, type);
}
- C#通用类型转换 Convert.ChangeType
- C# Convert.ChangeType() 将未知类型转换为已知类型
- System.Convert.ChangeType 转换数据类型
- 强化System.Convert.ChangeType, 使其能够转换枚举ENUM和可空类型Nullable
- 强化System.Convert.ChangeType, 使其能够转换枚举ENUM和可空类型Nullable
- c#中Convert.ChangeType的意义
- C#编程--convert类型转换
- C#通用类型转换方法
- 将一个值的类型转化成与其属性对应的类型Convert.ChangeType
- Convert.ChangeType在反射应用
- 类型转换Convert
- 【C#编程基础学习笔记】4---Convert类型转换
- SQL convert 时间类型转换
- Mysql CONVERT类型转换功能
- 通用类型转换方法
- 通用类型转换(template)
- C#通用类型转换器
- c#类型转换(Convert.ToInt32与Int32.Parse,Int32.TryParse的区别和性能)
- 【rmzt】美女之&&目露凶光win7主题
- object-c中NSString与int和float的相互转换
- 试图执行系统不支持的操作,问题
- [Perl]用log4perl记录日志
- 死锁原因及解决、避免办法
- C#通用类型转换 Convert.ChangeType
- linux常用命令
- ajax请求服务器数据包含中文处理以及MVC中下载文件名在ie和firefox中的处理
- Mono on CentOS 6.3 安装笔记
- android下载大图片避免OOM的解决方法
- 响应式布局这件小事
- linux文件打包命令
- u-boot命令
- 研究生读写论文的方法