C#类型转换
来源:互联网 发布:分布式数据库 知乎 编辑:程序博客网 时间:2024/05/16 07:36
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);
}
0 0
- C#中的类型转换
- C#中的类型转换
- C#中的类型转换
- C#中的类型转换
- C#中的类型转换
- c#中的类型转换
- C#中的类型转换
- C#中的类型转换
- C#类型转换
- C#中的类型转换
- C# 类型转换
- C#类型转换
- C# 中的类型转换
- C# 中的类型转换
- C# 中的类型转换
- C#中的类型转换
- C#中的类型转换
- C#中的类型转换
- C++中Static作用和使用方法
- Codeforces 468C Hack it!(数学)
- 关于ACM的输入输出
- IAP51修改例程操作SPI闪存
- POJ 3086 Triangular Sums(水题)
- C#类型转换
- java多态
- 操作系统知识
- HDU 1028 Ignatius and the Princess III
- 数据结构 第二章 线性表
- Ubuntu 使用Git 使用
- C#判断物体是否会在水中下沉
- c#初学
- 求两个数的最小公倍数