亲宝软件园·资讯

展开

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

人气:1
想了解C#中判断某类型是否可以进行隐式类型转换的相关内容吗,在本文为您仔细讲解隐式类型转换的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:隐式,类型,转换,下面大家一起来学习吧。

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

加载全部内容

相关教程
猜你喜欢
用户评论