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

  • 相关阅读:
    Vue总结
    Android适配总结
    Java为什么需要四种引用?
    回溯递归:八皇后
    eclipse中的maven build 、maven clean 、 maven install作用
    JS时间格式CST转GMT
    什么是ECMAScript、什么又是ECMA?
    maven打包工程出现错误 Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
    Vue三步完成跨域请求
    Eclipse导入别人项目爆红叉
  • 原文地址:https://www.cnblogs.com/wdfrog/p/1749713.html
Copyright © 2011-2022 走看看