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

  • 相关阅读:
    读书小记--<态度>
    frp 使用基础笔记
    ACM ICPC 2018 青岛赛区 部分金牌题题解(K,L,I,G)
    简单粗暴!解决锐捷强制关闭VMware NAT Service的问题(图文教程)
    杜教筛使用心得
    在阿里云的轻量级服务器上装桌面
    2018多校第三场 hdu6331 M :Walking Plan
    数论题集合
    hihoCoder挑战赛34 B题(快速求第k轮冒泡排序的结果)
    一阶微分边缘检测算子
  • 原文地址:https://www.cnblogs.com/mxw09/p/1888075.html
Copyright © 2020-2023  润新知