Nullable 類型的轉換

今天碰到Nullable 不能通過Convert.ChangeType轉換,辛苦在網上找到兩個解決方法,共享一下。
1.The PumaCode.org Blog
None.gifpublic object ChangeType(object value, Type conversionType)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
if ( conversionType.IsGenericType &&
ExpandedSubBlockStart.gifContractedSubBlock.gif        conversionType.GetGenericTypeDefinition( ).Equals( 
typeof( Nullable<> ) ) ) dot.gif{
InBlock.gif 
InBlock.gif        
if(value == null)
InBlock.gif            
return null;
InBlock.gif 
InBlock.gif        System.ComponentModel.NullableConverter nullableConverter
InBlock.gif            
= new System.ComponentModel.NullableConverter(conversionType);
InBlock.gif 
InBlock.gif        conversionType 
= nullableConverter.UnderlyingType;
ExpandedSubBlockEnd.gif    }

InBlock.gif 
InBlock.gif    
return Convert.ChangeType(value, conversionType);
ExpandedBlockEnd.gif}
引用:http://blog.pumacode.org/2006/05/18/using-convert-changetype-on-nullable-types/

2.Paul Wilson's .NET Blog
None.gifpublic class DataTypeConverter
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public static object ChangeType(Type type,object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ((value == null&& type.IsGenericType)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Activator.CreateInstance(type);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (value == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (type == value.GetType())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (type.IsEnum)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (value is string)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return Enum.Parse(type, value as string);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return Enum.ToObject(type, value);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (!type.IsInterface && type.IsGenericType)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Type type1 
= type.GetGenericArguments()[0];
InBlock.gif                
object obj1 = DataTypeConverter.ChangeType(type1,value);
ExpandedSubBlockStart.gifContractedSubBlock.gif                
return Activator.CreateInstance(type, new object[] dot.gif{ obj1 });
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ((value is string&& (type == typeof(Guid)))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new Guid(value as string);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ((value is string&& (type == typeof(Version)))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new Version(value as string);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (!(value is IConvertible))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return Convert.ChangeType(value, type);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
這個代碼是WilsonORMapper中的QueryHelper類,不好意思,我Reflector了一下。
引用:http://weblogs.asp.net/pjohnson/archive/2006/02/07/437631.aspx
發佈了7 篇原創文章 · 獲贊 0 · 訪問量 3004
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章