• C# 会可能需要的扩展


     1. List 转成DataSet

            /// <summary>
            /// 集合数据转成 DataSet
            /// </summary>
            /// <typeparam name="TSource"></typeparam>
            /// <param name="list"></param>
            /// <returns></returns>
            public static DataSet ToDataSet<TSource>(this IList<TSource> list)
            {
                Type elementType = typeof(TSource);
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                ds.Tables.Add(dt);
    
                foreach (var pi in elementType.GetProperties())
                {
                    Type colType = Nullable.GetUnderlyingType(pi.PropertyType) ?? pi.PropertyType;
                    dt.Columns.Add(pi.Name, colType);
                }
    
                foreach (TSource item in list)
                {
                    DataRow row = dt.NewRow();
                    foreach (var pi in elementType.GetProperties())
                    {
                        row[pi.Name] = pi.GetValue(item, null) ?? DBNull.Value;
                    }
                    dt.Rows.Add(row);
                }
    
                return ds;
            }
    
            public static DataSet ToDataSet<TSource>(this ICollection<TSource> list)
            {
                Type elementType = typeof(TSource);
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                ds.Tables.Add(dt);
    
                foreach (var pi in elementType.GetProperties())
                {
                    Type colType = Nullable.GetUnderlyingType(pi.PropertyType) ?? pi.PropertyType;
                    dt.Columns.Add(pi.Name, colType);
                }
    
                foreach (TSource item in list)
                {
                    DataRow row = dt.NewRow();
                    foreach (var pi in elementType.GetProperties())
                    {
                        row[pi.Name] = pi.GetValue(item, null) ?? DBNull.Value;
                    }
                    dt.Rows.Add(row);
                }
    
                return ds;
            }

     2. 枚举扩展函数

            /// <summary>
            /// 枚举扩展函数
            /// <para>把枚举转换成对应的byte 类型,再转换成String</para>
            /// </summary>
            /// <param name="en"></param>
            /// <returns></returns>
            public static string ToByteString(this Enum en)
            {
                return ((byte)(dynamic)en).ToString();
            }

     3. Md5加密函数    

            /// <summary>
            ///     Md5加密函数
            /// </summary>
            /// <param name="strPwd"></param>
            /// <returns></returns>
            public static string Encrypt(string strPwd)
            {
                var md5Hasher = MD5.Create();
    
                var data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(strPwd)); //将字符编码为一个字节序列 
    
                var sBuilder = new StringBuilder(); //计算data字节数组的哈希值
    
                foreach (var @byte in data)
                {
                    sBuilder.Append(@byte.ToString("x2"));
                }
    
                return sBuilder.ToString();
            }
         

    4.MVC 扩展

                    /// <summary>
            ///  MVC 扩展
            /// </summary>
            /// <param name="parer">The parer.</param>
            /// <param name="subjectType">Type of the subject.</param>
            /// <returns></returns>
            public static MvcHtmlString MvcHtmlStringExpand(string parer, byte subjectType)
            {
                var html = "<h3>这是一个扩展方法</h3>";
    
            
                return MvcHtmlString.Create(html);
            }

    5.字符串转换成byte

            /// <summary>
            /// 字符串转换成byte
            /// </summary>
            /// <param name="val"></param>
            /// <returns></returns>
            public static byte ToByte(this string val)
            {
                if (string.IsNullOrEmpty(val))
                {
                    return 0;
                }
                var intVal = int.Parse(val);
    
                return (byte)intVal;
            }

     6.字符串转换成int16

            /// <summary>
            /// 字符串转换成int16
            /// </summary>
            /// <param name="val"></param>
            /// <returns></returns>
            public static short ToInt16(this string val)
            {
                if (string.IsNullOrEmpty(val))
                {
                    return 0;
                }
                var intVal = int.Parse(val);
    
                return (short)intVal;
            }

    7.字符串是否为空

            /// <summary>
            /// 字符串是否为空
            /// </summary>
            /// <param name="val"></param>
            /// <returns></returns>
            public static bool IsNull(this string val)
            {
                return string.IsNullOrEmpty(val);
            }

    8.字符串是不为空,有值状态

            /// <summary>
            /// 字符串是不为空,有值状态
            /// </summary>
            /// <param name="val"></param>
            /// <returns></returns>
            public static bool IsNotNull(this string val)
            {
                return !string.IsNullOrEmpty(val);
            }
  • 相关阅读:
    六度分离_hdu_1869(floyd算法).java
    MFC版的Hello World
    Google Protobuf的安装、配置、以及简单demo编译
    在Android工程中加入AIDL文件时,gen目录生成的文件报错-问题解决
    SharePoint 2013的100个新功能之网站管理(二)
    SharePoint 2013的100个新功能之网站管理(一)
    SharePoint 2013的100个新功能之社交
    SharePoint 2013的100个新功能之内容管理(四)
    SharePoint 2010 Ribbon with wrong style in Chrome and Safari
    SharePoint 2013的100个新功能之内容管理(三)
  • 原文地址:https://www.cnblogs.com/Hangle/p/6215766.html
Copyright © 2020-2023  润新知