zoukankan      html  css  js  c++  java
  • Nullable 类型的转换

    今天碰到Nullable 不能通过Convert.ChangeType转换,辛苦在网上找到两个解决方法,共享一下。
    1.The PumaCode.org Blog
    public object ChangeType(object value, Type conversionType)
    {
        
    if ( conversionType.IsGenericType &&
            conversionType.GetGenericTypeDefinition( ).Equals( 
    typeof( Nullable<> ) ) ) {
     
            
    if(value == null)
                
    return null;
     
            System.ComponentModel.NullableConverter nullableConverter
                
    = new System.ComponentModel.NullableConverter(conversionType);
     
            conversionType 
    = nullableConverter.UnderlyingType;
        }

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

    2.Paul Wilson's .NET Blog
    public class DataTypeConverter
        
    {
            
    public static object ChangeType(Type type,object value)
            
    {
                
    if ((value == null&& type.IsGenericType)
                
    {
                    
    return Activator.CreateInstance(type);
                }

                
    if (value == null)
                
    {
                    
    return null;
                }

                
    if (type == value.GetType())
                
    {
                    
    return value;
                }

                
    if (type.IsEnum)
                
    {
                    
    if (value is string)
                    
    {
                        
    return Enum.Parse(type, value as string);
                    }

                    
    return Enum.ToObject(type, value);
                }

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

                
    if ((value is string&& (type == typeof(Guid)))
                
    {
                    
    return new Guid(value as string);
                }

                
    if ((value is string&& (type == typeof(Version)))
                
    {
                    
    return new Version(value as string);
                }

                
    if (!(value is IConvertible))
                
    {
                    
    return value;
                }

                
    return Convert.ChangeType(value, type);
            }

        }
    这个代码是WilsonORMapper中的QueryHelper类,不好意思,我Reflector了一下。
    引用:http://weblogs.asp.net/pjohnson/archive/2006/02/07/437631.aspx
  • 相关阅读:
    在UltraEdit中如何像NotePad++一样实现双击单词在全文中高亮
    记人生第一次做面试官的经历
    error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MDd_DynamicDebug
    压缩感知中的数学知识:稀疏、范数、符号arg min
    Tensorflow timeline trace
    tensorflow serving
    日志分析工具ELK(一)
    Zabbix3.0安装部署最佳实践
    防cc攻击利器之Httpgrard
    反向代理负载均衡之haproxy
  • 原文地址:https://www.cnblogs.com/maplye/p/443391.html
Copyright © 2011-2022 走看看