C#中判断某类型是否可以进行隐式类型转换
发布时间:2023-03-28 15:29:36
标签:隐式,类型,转换
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;
}
0
投稿
猜你喜欢
- 本文实例为大家分享了使用PageHelper插件实现Service层分页的具体代码,供大家参考,具体内容如下使用场景:平时分页我们可以直接使
- 有时候需要根据条件查询得出的数据较多,需要分页显示到页面上。这时点击下一页就不方便每次带查询条件在数据库中分页。可以在list中进行分页。p
- 一、Java的前世为什么会产生Java?Java的特点是什么?从C语言开始讲,C语言是一种结构化语言,模块化编程,便于程序的调试,依靠非常全
- 1.向上转型 向下转型2.强制类型转换的应用应用多态性时由于引用为父类类型,导致编译时只能调用父类中声明的属性和方法。子类特有的属性和方法不
- 最近做项目,ORM 使用的是 MyBatis,为了偷懒,我自然而然的想到了使用 MyBatis Generator(MBG)来生成数据库表对
- java.util 包提供了 Date 类来封装当前的日期和时间。 Date 类提供两个构造函数来实例化 Date 对象。第一个构造函数使用
- 本文主要介绍了java中LinkedList使用迭代器优化移除批量元素原理,分享给大家,具体如下:public interface Iter
- 一、点睛邻接矩阵通常采用一个一维数组存储图中节点的信息,采用一个二维数组存储图中节点之间的邻接关系。邻接矩阵可以用来表示无向图、有向图和网。
- Java实现简单的类似QQ聊天工具,供大家参考,具体内容如下所使用到的知识点:java socket编程之TCP协议java Swing简单
- java如何实现ftp上传?如何创建文件夹?最佳答案:准备条件:java实现ftp上传用到了commons-net-3.3.jar包首先建立
- 快速排序算法概念快速排序一般基于递归实现。其思路是这样的:1.选定一个合适的值(理想情况中值最好,但实现中一般使用数组第一个值),称为“枢轴
- package cn.response;import java.awt.Color;import java.awt.Font;import
- 前言本文准确来讲是探讨如何用 Jackson 来序列化 Apache avro 对象,因为简单用 Jackson 来序列化 Apache a
- public class CrossSum{ public static void main(String args[]){
- Visual Studio 2022 默认.net framework4.8,而4.6~4.7版本的.net framework可以通过方法
- 利用Android的ApiDemos的Rotate3dAnimation实现了个图片3D旋转的动画,围绕Y轴进行旋转,还可以实现Z轴的缩放。
- 经过几天的折腾,终于到了学习一个重量级的查询方式上,使用@Query注解,使用注解有两种方式,一种是JPQL的SQL语言方式,一种是原生SQ
- 高并发访问时,缓存、限流、降级往往是系统的利剑,在互联网蓬勃发展的时期,经常会面临因用户暴涨导致的请求不可用的情况,甚至引发连锁反映导致整个
- 本篇给大家详细讲解了MTKAndroid平台开发流程,大致分为44个步骤,我们把每个步骤的命令详细讲解了下,一起来学习下。1.拷贝代码仓库从
- 1,添加依赖在project的build.gradle文件中添加dependencies { classpath 'co