判断C#中可以进行隐式类型转换的类型
2011-11-21 13:15
453 查看
在我们采用反射动态调用一些方法时,常常涉及到类型的转换,直接判断类型是否相符有时不能判断调用方法是否合适
C#中,有些类型是可以隐式转换的,我整理了这些可以隐式转换的类型,供大家参考
C#中,有些类型是可以隐式转换的,我整理了这些可以隐式转换的类型,供大家参考
static private bool CanConvert(Type from, Type to) { if (from.IsPrimitive && to.IsPrimitive) { TypeCode typeCodeFrom = Type.GetTypeCode(from); TypeCode typeCodeTo = Type.GetTypeCode(to); if (typeCodeFrom == typeCodeTo) return true; if (typeCodeFrom == TypeCode.Char) switch (typeCodeTo) { case TypeCode.UInt16: return true; case TypeCode.UInt32: return true; case TypeCode.Int32: return true; case TypeCode.UInt64: return true; case TypeCode.Int64: return true; case TypeCode.Single: return true; case TypeCode.Double: return true; default: return false; } // Possible conversions from Byte follow. if (typeCodeFrom == TypeCode.Byte) switch (typeCodeTo) { case TypeCode.Char: return true; case TypeCode.UInt16: return true; case TypeCode.Int16: return true; case TypeCode.UInt32: return true; case TypeCode.Int32: return true; case TypeCode.UInt64: return true; case TypeCode.Int64: return true; case TypeCode.Single: return true; case TypeCode.Double: return true; default: return false; } // Possible conversions from SByte follow. if (typeCodeFrom == TypeCode.SByte) switch (typeCodeTo) { case TypeCode.Int16: return true; case TypeCode.Int32: return true; case TypeCode.Int64: return true; case TypeCode.Single: return true; case TypeCode.Double: return true; default: return false; } // Possible conversions from UInt16 follow. if (typeCodeFrom == TypeCode.UInt16) switch (typeCodeTo) { case TypeCode.UInt32: return true; case TypeCode.Int32: return true; case TypeCode.UInt64: return true; case TypeCode.Int64: return true; case TypeCode.Single: return true; case TypeCode.Double: return true; default: return false; } // Possible conversions from Int16 follow. if (typeCodeFrom == TypeCode.Int16) switch (typeCodeTo) { case TypeCode.Int32: return true; case TypeCode.Int64: return true; case TypeCode.Single: return true; case TypeCode.Double: return true; default: return false; } // Possible conversions from UInt32 follow. if (typeCodeFrom == TypeCode.UInt32) switch (typeCodeTo) { case TypeCode.UInt64: return true; case TypeCode.Int64: return true; case TypeCode.Single: return true; case TypeCode.Double: return true; default: return false; } // Possible conversions from Int32 follow. if (typeCodeFrom == TypeCode.Int32) switch (typeCodeTo) { case TypeCode.Int64: return true; case TypeCode.Single: return true; case TypeCode.Double: return true; default: return false; } // Possible conversions from UInt64 follow. if (typeCodeFrom == TypeCode.UInt64) switch (typeCodeTo) { case TypeCode.Single: return true; case TypeCode.Double: return true; default: return false; } // Possible conversions from Int64 follow. if (typeCodeFrom == TypeCode.Int64) switch (typeCodeTo) { case TypeCode.Single: return true; case TypeCode.Double: return true; default: return false; } // Possible conversions from Single follow. if (typeCodeFrom == TypeCode.Single) switch (typeCodeTo) { case TypeCode.Double: return true; default: return false; } } return false; }
相关文章推荐
- C#中判断某类型是否可以进行隐式类型转换
- 判断C#中可以进行隐式类型转换的类型
- c#下怎么判断一个字符串是否可以转换为double类型
- C#根据输入框的字符判断可以转换为那些数据类型
- 测试两个类型是否可以进行隐式转换
- c# 隐式类型 转换注意点
- C#隐式和显示类型转换
- 在C#中对于null/DBNull.Value值进行类型转换时,除了Convert.ToString()之外,其他的转换都会报空引用异常;
- c++设计中可以利用重载避免隐式类型转换
- C#中使用as进行类型转换
- C# 泛型 无法将类型xx隐式转换为“T”
- C#的类型转换(1)之隐式和显式转换
- 判断字符串是否可以转换成日期类型
- C#之隐式与显示类型转换
- C# 隐式类型转换
- C#先把数值类型转换为带千分位的字符串,再判断是否带小数,如果带小数,保存两位小数,不带,不保留。
- 判断C#中的字符串是否是数字,如果是转换成int类型
- 在进行C#编程时候,有的时候我们需要判断一个字符串是否是数字字符串,我们可以通过以下两种方法来实现。 【方法一】:使用 try{} catch{} 语句。 我们可以在try语句块中试图
- 那些数据可以通过隐式转换成布尔Boolean类型数据?最全面总结,终极版!!!
- C#中数据类型转换-隐式转换