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

  • 相关阅读:
    恭喜你,你毕业了
    用VB.Net2008制作安装程序详细步骤(菜鸟级别,高手勿进)
    交通标志结构计算软件开发进程
    【工作笔记002】在TC中建立应用于出行分布的阻抗矩阵(最短路矩阵)
    VB.Net 2008 引用Excel12
    开博,开播。
    【推荐】万物兴歇——衰老与寿命的演化
    一张交叉口渠划的彩色平面图
    萦绕在头脑中的思路_我的编程梦们 【更新至2010.06.03】
    8月份的回顾
  • 原文地址:https://www.cnblogs.com/mxw09/p/1888075.html
Copyright © 2011-2022 走看看