欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中判断某类型是否可以进行隐式类型转换

程序员文章站 2023-12-18 13:33:10
c#中,有些类型是可以隐式转换的,我整理了这些可以隐式转换的类型,供大家参考复制代码 代码如下:     static privat...

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;
   }

上一篇:

下一篇: