zoukankan      html  css  js  c++  java
  • 泛型摘录

    一段类型转化

    --------------------------------------------------------------------

     /// <summary>
                /// Gets the value.
                /// </summary>
                /// <typeparam name="T"></typeparam>
                /// <param name="columnName">Name of the column.</param>
                /// <returns></returns>
                public T GetValue<T>(string columnName)
                {
                    columnName = columnName.ToLowerInvariant();
                    object oVal;

                    try
                    {
                        oVal = this[columnName].CurrentValue;
                    }
                    catch
                    {
                        throw new ArgumentException("There's no column called '" + columnName + "' for this object", "columnName");
                    }

                    if(oVal == null || oVal == DBNull.Value)
                        return default(T);

                    Type type = typeof(T);

                    if(type == typeof(object))
                        return (T)oVal;

                    // handle nullable type conversion
                    if(IsNullable(type))
                    {
                        NullableConverter converter = new NullableConverter(type);
                        type = converter.UnderlyingType;
                    }

                    //if (IsNullable(type) || type == typeof(object))
                    //{
                    //    if (type == typeof(bool?))
                    //        return (T)(object)Convert.ToBoolean(oVal);
                    //    return (T)oVal;
                    //}

                    //if (type == typeof(Guid))
                    //    return (T)(object)new Guid(oVal.ToString());

                    Type valType = oVal.GetType();
                    if(valType == typeof(Byte[]))
                        return (T)Convert.ChangeType(oVal, valType);

                    return (T)Convert.ChangeType(oVal, type);
                }

  • 相关阅读:
    gjrand 4.0 发布,C语言的伪随机数生成器
    Kite 0.2.0 发布,编程语言
    IBM/DW C++11 标准新特性:Defaulted 和 Deleted 函数
    Apache Solr 3.6.2 发布
    bitmap 内存溢出处理
    Qtractor 0.5.7 发布,多轨音序生成器
    CUBRID Node.js Driver 1.1 发布
    OrientDB 1.3.0 发布,基于文档的数据库
    linux centos 下面httpd支持的svn 服务器端安装
    iptables 1.4.17 发布,Linux防火墙
  • 原文地址:https://www.cnblogs.com/wdfrog/p/1749713.html
Copyright © 2011-2022 走看看