C# 會可能須要的擴展

 1. List 轉成DataSethtml

        /// <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 擴展ui

                /// <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.字符串轉換成bytethis

        /// <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.字符串是不爲空,有值狀態spa

        /// <summary>
        /// 字符串是不爲空,有值狀態
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static bool IsNotNull(this string val)
        {
            return !string.IsNullOrEmpty(val);
        }
相關文章
相關標籤/搜索