zoukankan      html  css  js  c++  java
  • DataRow的泛型扩展方法

    最近发现以前写的下面两个方法都有错误,default(string) 是null ,null is string 返回false

     static class Extend

        {
            const string def = "dd";
            public static T GetValue<T>(this DataRow dr, string name)
            {
                try
                {
                    if (dr[name] == DBNull.Value)
                    {
                        object o = def;
                        return (T)o;
                    }
                    else
                    {
                        return (T)dr[name];
                    }
                }
                catch (ArgumentException ex)
                {
                    T t = default(T);
                    object obj;
                    if (t is ValueType)
                    {
                        obj = t;
                    }
                    else if (t is string)
                    {
                        obj = def;
                    }
                    else
                    {
                        obj = t;
                    }
                    return (T)obj;
                }

                //if (dr[name] == DBNull.Value)
                //{
                //    T item = default(T);
                //    object obj;
                //    if (item is ValueType)
                //    {
                //        obj = 0;

                //    }
                //    else
                //    {
                //        obj = "";
                //    }
                //    return (T)obj;
                //}
                //else
                //{
                //    T res = (T)dr[name];
                //    return res;
                //}
            }

        }

    public static class MyExtend
        {

            public static T GetValue<T>(this DataRow dr, string name)
            {
                try
                {
                    if (dr[name] == DBNull.Value)
                    {                    
                        return GetDefault<T>();
                    }
                    else
                    {
                        return (T)dr[name];
                    }
                }
                catch (ArgumentException ex)
                {
                    return GetDefault<T>();
                }
            }

            static T GetDefault<T>()
            {
                T t = default(T);
                object obj;
                if (t is string)
                {
                    obj = "";
                }
                else
                {
                    obj = t;
                }
                return (T)obj;
            }
        } 

  • 相关阅读:
    分分钟提升命令行模式下密码输入逼格
    MySQL server has gone away 的两个最常见的可能性
    第一次遇到刷新缓冲区延时
    Mac上安装mysqlclient的报错
    python3 --- locale命名空间让程序更加安全了
    doctest --- 一个改善python代码质量的工具
    MySQL优化器 --- index_merge
    机智的MySQL优化器 --- is null
    Centos-7.x 下子网掩码的配置
    JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(三):两个Viewmodel搞定增删改查
  • 原文地址:https://www.cnblogs.com/mxw09/p/1888075.html
Copyright © 2011-2022 走看看