zoukankan      html  css  js  c++  java
  • 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
    }
  • 相关阅读:
    第二篇 Flask 中的 Render Redirect HttpResponse
    第一篇 你好,我叫Flask
    redis发布订阅
    redis学习
    mysql+centos7+主从复制
    Linux系统基础优化及常用命令
    vim与程序员
    Shell基本命令
    js bom和dom
    javaScript基础
  • 原文地址:https://www.cnblogs.com/itjeff/p/4269766.html
Copyright © 2011-2022 走看看