C#记录日志、获取枚举值 等通用函数列表

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; namespace System.Web.Mvc
{
    #region 通用函数
    /// <summary>
    /// 通用函数
    /// </summary>
    public class Common
    {
        #region IP地址转换
        /// <summary>
        /// 将IP地址转换为数值
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static long Ip2Long(string ip)
        {
            var ipaddr = ip.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            if (ipaddr.Length == 4)
            {
                var ipvals = ipaddr.Select(p => int.Parse(p)).ToList();
                if (ipvals[0] >= 0 && ipvals[0] <= 255
                && ipvals[1] >= 0 && ipvals[1] <= 255
                && ipvals[2] >= 0 && ipvals[2] <= 255
                && ipvals[3] >= 0 && ipvals[3] <= 255)
                    return (long)ipvals[0] << 24 + ipvals[1] << 16 + ipvals[2] << 8 + ipvals[3];
            }
            return -1;
        }         /// <summary>
        /// 将IP地址转换为字符串
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static string Ip2String(long ip)
        {
            var ipvals = "";
            ipvals = (ip % 256).ToString();
            ip = ip / 256;
            ipvals = (ip % 256).ToString() + "." + ipvals;
            ip = ip / 256;
            ipvals = (ip % 256).ToString() + "." + ipvals;
            ipvals = (ip / 256).ToString() + "." + ipvals;
            return ipvals;
        }         /// <summary>
        /// Server.PapPath()重写
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string MapPath(string path)
        {
            var currentDir = AppDomain.CurrentDomain.BaseDirectory;
            path = path.Replace("~", currentDir);
            path = System.IO.Path.GetFullPath(path);
            return path;
        }         public static void Log(Exception ex)
        {
            Log(ex.ToString());
        }         public static void Log(string ex)
        {
            var fd = Common.MapPath("~/Logs/");
            if (!System.IO.Directory.Exists(fd)) System.IO.Directory.CreateDirectory(fd);             var fp = fd + string.Format("l_{0:yyyyMMddHH}.log", DateTime.Now);
            using (var f = System.IO.File.AppendText(fp))
            {
                f.Write(ex);
            }
        }
        #endregion         #region 枚举通用函数
        /// <summary>
        /// 获取枚举类型列表
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <returns></returns>
        public static SortedList GetEnumList<T>()
        {
            var t = typeof(T);
            SortedList sl = new SortedList();
            Array a = Enum.GetValues(t);
            for (int i = 0; i < a.Length; i++)
            {
                string name = a.GetValue(i).ToString();
                int key = (int)a.GetValue(i);
                string desc = GetEnumDesc<T>(key);
                sl.Add(key, desc);
            }
            return sl;
        }         /// <summary>
        /// 获取枚举类型名称
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="enumValue">枚举值</param>
        /// <returns></returns>
        public static string GetEnumName<T>(object enumValue)
        {
            try
            {
                return Enum.GetName(typeof(T), enumValue);
            }
            catch
            {
                return "NA";
            }
        }         /// <summary>
        /// 获取枚举类型描述
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="enumValue">枚举值</param>
        /// <returns></returns>
        public static string GetEnumDesc<T>(object enumValue)
        {
            try
            {
                var t = typeof(T);
                DescriptionAttribute[] attrs = (DescriptionAttribute[])t.GetField(GetEnumName<T>(enumValue)).GetCustomAttributes(typeof(DescriptionAttribute), false);
                return ((attrs.Length > 0) ? attrs[0].Description : GetEnumName<T>(enumValue));
            }
            catch
            {
                return "NA";
            }
        }
        #endregion             }
    #endregion
}
上一篇:EF框架的三种模式


下一篇:Python列表(一)