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

  • 相关阅读:
    gzip 压缩格式的网站处理方法---sina.com 分类: python python基础学习 2013-07-16 17:40 362人阅读 评论(0) 收藏
    自定义系统命令缩写 分类: ubuntu 2013-07-15 17:42 344人阅读 评论(0) 收藏
    线程 ing 分类: python 2013-07-15 14:28 197人阅读 评论(0) 收藏
    [模板]排序
    [BFS] [洛谷] P1032 字串变换
    [二分答案][洛谷] P1316 丢瓶盖
    [二分] [POJ] 2456 Aggressive cows
    [贪心] [STL] [51nod] 做任务三
    [BFS] [记忆化] [洛谷] P1141 01迷宫
    [DFS] [记忆化] [洛谷] P1434 [SHOI2002]滑雪
  • 原文地址:https://www.cnblogs.com/mxw09/p/1888075.html
Copyright © 2011-2022 走看看