C#类型转换2
2012-09-15 16:55
351 查看
namespace WDBuyNET.DMSFrame.Utils.Helpers { public static class TypeHelper { public static object ChangeType(Type targetType, object val) { object result; if (val == null) { result = null; } else { if (targetType.IsAssignableFrom(val.GetType())) { result = val; } else { if (targetType == val.GetType()) { result = val; } else { if (targetType == typeof(bool)) { if (val.ToString() == "0") { result = false; return result; } if (val.ToString() == "1") { result = true; return result; } } if (targetType.IsEnum) { int num = 0; result = (int.TryParse(val.ToString(), out num) ? val : Enum.Parse(targetType, val.ToString())); } else { result = ((!(targetType == typeof(Type))) ? ((!(targetType == typeof(IComparable))) ? Convert.ChangeType(val, targetType) : val) : ReflectionHelper.GetType(val.ToString())); } } } } return result; } public static string GetClassSimpleName(Type t) { string[] array = t.ToString().Split(new char[] { '.' }); return array[array.Length - 1].ToString(); } public static object GetDefaultValue(Type destType) { return (!TypeHelper.IsNumbericType(destType)) ? ((!(destType == typeof(string))) ? ((!(destType == typeof(bool))) ? ((!(destType == typeof(DateTime))) ? ((!(destType == typeof(Guid))) ? ((!(destType == typeof(TimeSpan))) ? null : TimeSpan.Zero) : Guid.NewGuid()) : DateTime.Now) : false) : "") : 0; } public static string GetDefaultValueString(Type destType) { return (!TypeHelper.IsNumbericType(destType)) ? ((!(destType == typeof(string))) ? ((!(destType == typeof(bool))) ? ((!(destType == typeof(DateTime))) ? ((!(destType == typeof(Guid))) ? ((!(destType == typeof(TimeSpan))) ? "null" : "System.TimeSpan.Zero") : "System.Guid.NewGuid()") : "DateTime.Now") : "false") : "\"\"") : "0"; } public static Type GetTypeByRegularName(string regularName) { return ReflectionHelper.GetType(regularName); } public static string GetTypeRegularName(Type destType) { string arg1 = destType.Assembly.FullName.Split(new char[] { ',' })[0]; return string.Format("{0},{1}", destType.ToString(), arg1); } public static string GetTypeRegularNameOf(object obj) { return TypeHelper.GetTypeRegularName(obj.GetType()); } public static bool IsFixLength(Type destDataType) { bool arg_46_0; if (!TypeHelper.IsNumbericType(destDataType)) { if (!(destDataType == typeof(byte[]))) { arg_46_0 = (destDataType == typeof(DateTime) || destDataType == typeof(bool)); } else { arg_46_0 = true; } } else { arg_46_0 = true; } return arg_46_0; } public static bool IsIntegerCompatibleType(Type destDataType) { bool arg_92_0; if (!(destDataType == typeof(int)) && !(destDataType == typeof(uint)) && !(destDataType == typeof(short)) && !(destDataType == typeof(ushort)) && !(destDataType == typeof(long)) && !(destDataType == typeof(ulong)) && !(destDataType == typeof(byte))) { arg_92_0 = (destDataType == typeof(sbyte)); } else { arg_92_0 = true; } return arg_92_0; } public static bool IsNumbericType(Type destDataType) { bool arg_D1_0; if (!(destDataType == typeof(int)) && !(destDataType == typeof(uint)) && !(destDataType == typeof(double)) && !(destDataType == typeof(short)) && !(destDataType == typeof(ushort)) && !(destDataType == typeof(decimal)) && !(destDataType == typeof(long)) && !(destDataType == typeof(ulong)) && !(destDataType == typeof(float)) && !(destDataType == typeof(byte))) { arg_D1_0 = (destDataType == typeof(sbyte)); } else { arg_D1_0 = true; } return arg_D1_0; } public static bool IsSimpleType(Type t) { bool arg_7B_0; if (!TypeHelper.IsNumbericType(t)) { if (!(t == typeof(char))) { if (!(t == typeof(string))) { if (!(t == typeof(bool))) { if (!(t == typeof(DateTime))) { arg_7B_0 = (t == typeof(Type) || t.IsEnum); } else { arg_7B_0 = true; } } else { arg_7B_0 = true; } } else { arg_7B_0 = true; } } else { arg_7B_0 = true; } } else { arg_7B_0 = true; } return arg_7B_0; } } }
相关文章推荐
- SQL数据类型和C#数据类型间的转换
- C#中使用指针转换数据类型[C#/unsafe]
- C#隐式和显示类型转换
- C# string类型和byte[]类型相互转换
- C# Convert.ChangeType() 将未知类型转换为已知类型
- 语言基础:C#输入输出与数据类型及其转换
- C#运算符与数据类型转换
- C#的数据类型转换
- C#学习笔记(基础知识回顾)之值类型与引用类型转换(装箱和拆箱)
- C#控制台基础 可空类型与非可空类型 相互转换
- C#学习笔记--引用类型分类,对象类型、字符串类型、数字类型,类型转换
- C#调用C++版本dll时的类型转换需要注意的问题小结
- 【DotNet 技能系列】3. C#中算术运算符、赋值运算符与类型转换
- 【DotNet 技能系列】4. C#中数据类型转换
- C#基础知识(二)——字符与类型转换
- C# 将一个对象转换为指定类型
- C#.NET 中的类型转换
- C#学习第三天 类型转换和随机数
- 把Sql类型转换为C#类型的函数
- C# 类型“System.String”的对象无法转换为类型“System.Int32”