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
}
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
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)