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
}
复制代码

 

posted on   itjeff  阅读(521)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示