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
}
C#记录日志、获取枚举值 等通用函数列表的更多相关文章
- 获取枚举值上的Description特性说明
/// <summary> /// 获取枚举值上的Description特性说明 /// </summary> /// <typeparam name="T&q ...
- c# 枚举的定义,枚举的用法,获取枚举值
1.定义枚举类型 public enum Test { 男 = , 女 = } 2.获取枚举值 public void EnumsAction() { var s = Test.男;//男 var a ...
- C# .NET 获取枚举值的自定义属性(特性/注释/备注)信息
一.引言 枚举为我看日常开发的可读性提供的非常好的支持,但是有时我们需要得到枚举值得描述信息或者是注释(备注)信息 比如要获得 TestEmun.aaa 属性值得备注 AAA,比较不方便得到. pub ...
- C# .NET 获取枚举值的自定义属性
一.定义一个类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- C# 获取枚举值/获取名字和值
枚举 int 转 枚举名称 public void Test() { //调用 string name1= ConvertEnumToString<ActionLogType>(1); s ...
- C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素
/// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtension { private stat ...
- OSS.Common获取枚举字典列表标准库支持
上篇(.Net Standard扩展支持实例分享)介绍了OSS.Common的标准库支持扩展,也列举了可能遇到问题的解决方案.由于时间有限,同时.net standard暂时还没有提供对Descrip ...
- c#枚举 获取枚举键值对、描述等
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.C ...
- c#枚举值增加特性说明
c#枚举值增加特性说明 通过特性给一个枚举类型每个值增加一个字符串说明,用于打印或显示. 自定义打印特性 [AttributeUsage(AttributeTargets.Field)] public ...
随机推荐
- HYSBZ 2243 染色 (树链拆分)
主题链接~~> 做题情绪:这题思路好想.调试代码调试了好久.第一次写线段树区间合并. 解题思路: 树链剖分 + 线段树区间合并 线段树的端点记录左右区间的颜色.颜色数目.合并的时候就用区间合并的 ...
- 【转】Directx11 HelloWorld之HLSL的Effect框架的使用
最近尝试用了下Directx下的Effect框架,作为一初学者初学者,说下为什么我们要使用Effect框架及其好处吧. 首先Effect最大好处的就是简单,使得编写Shader绘制的程序工作量大大下降 ...
- ssh: connect to host github.com port 22: Connection refused
假设git例如,下面的问题时,远程推送: [fulinux@ubuntu learngit]$ git push -u origin master ssh: connect to host githu ...
- 每天的学习经验:SharePoint 2013 定义自己添加的产品清单。Callout菜单项、文档关注、SharePoint服务机端对象模型查询
前言: 前一段时间一直都比較忙.没有什么时间进行总结,刚好节前项目上线.同一时候趁着放假能够好好的对之前遇到的一些问题进行总结. 主要内容有使用SharePoint服务端对象模型进行查询.为Share ...
- StackExchange.Redis 使用-配置 (四)
Configurationredis有很多不同的方法来配置连接字符串 , StackExchange.Redis 提供了一个丰富的配置模型,当调用Connect 或者 ConnectAsync 时需要 ...
- .NET应用架构设计—再次了解分层架构(现代企业应用分层架构核心设计元素)
阅读文件夹: 1.背景介绍 2.简要回想下传统三层架构 3.企业级应用分层架构(现代分层架构的基本演变过程) 3.1.服务层中应用契约式设计来解决动态条件不匹配错误(通过契约式设计模式来将问题在线下暴 ...
- poj 1274The Perfect Stall
第一次接触二分图匹配. 这题是一个匈牙利算法的模板题直接套即可. 题意是 给你奶牛和谷仓的个数a和b,接下来a行是奶牛喜欢去的谷仓.第一个是谷仓个数,接下来是谷仓编号. 这里我们把行当奶牛,列当谷仓 ...
- jquery ui tab跳转
1.tabs_iframe.jsp <%-- Document : tabs Created on : 2015-2-28, 14:44:02 Author : liyulin lyl01099 ...
- 【从零学习openCV】IOS7根据人脸检测
前言: 人脸检測与识别一直是计算机视觉领域一大热门研究方向,并且也从安全监控等工业级的应用扩展到了手机移动端的app.总之随着人脸识别技术获得突破,其应用前景和市场价值都是不可估量的,眼下在学习ope ...
- 实现android里面WebView显示内容
在日常学习.我们会看到,当手机应该检查注册协议.单击协议时,以查看内部协议的全部内容! 以下给大家看看实现的过程: 首先贴show_xy.XML代码: <WebView android:layo ...