判断C#中可以进行隐式类型转换的类型
来源:互联网 发布:windows系统即将过期 编辑:程序博客网 时间:2024/05/22 05:18
在我们采用反射动态调用一些方法时,常常涉及到类型的转换,直接判断类型是否相符有时不能判断调用方法是否合适
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#中进行类型转换
- 测试两个类型是否可以进行隐式转换
- java中instanceof判断类型是否可以转换
- java中 BigDecimal类型的可以转换到double类型
- C#中类型转换
- C#中类型转换
- C# 的类型转换
- C#的类型转换
- C#的类型转换
- C#的类型转换
- C#的类型转换
- C#类型的转换
- C#的类型转换
- C#的类型转换
- C# 中各种类型的相互转换
- C#中枚举类型的转换问题
- 1111
- C#中进程间的通信:管道
- 聚合参数中的语法错误: 需要具有可能的“Child”限定符的单个列参数。
- Hypertable一个 C++ 的Bigtable开源实现
- linux---软件包管理
- 判断C#中可以进行隐式类型转换的类型
- You say you want a revolution well you know we all want to change the world.——《乔布斯传》书摘
- java资讯
- Android SDK 各版本源代码下载
- VB 串口编程 开发心得
- J2EE技术(四)——JavaMail
- Error:System.Data.SqlClient.SqlException: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误,请检查远程连接是否开启。解决方法
- English address format
- Java篇之"HelloWorld"