CommonHelper.cs
6.85 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Sockets;
namespace HHECS.Infrastructure.CommonHelper
{
public static class CommonHelper
{
/// <summary>
/// 获取本地IP
/// </summary>
/// <returns></returns>
public static List<string> GetLocalIP()
{
try
{
string HostName = Dns.GetHostName(); //得到主机名
IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
return IpEntry.AddressList.Where(t => t.AddressFamily == AddressFamily.InterNetwork).Select(t => t.ToString()).ToList();
}
catch (Exception ex)
{
return null;
}
}
///// <summary>
///// 枚举转字典集合
///// </summary>
///// <typeparam name="T">枚举类名称</typeparam>
///// <param name="keyDefault">默认key值</param>
///// <param name="valueDefault">默认value值</param>
///// <returns>返回生成的字典集合</returns>
//public static Dictionary<string, object> EnumListDic<T>(string keyDefault, string valueDefault = "")
//{
// Dictionary<string, object> dicEnum = new Dictionary<string, object>();
// Type enumType = typeof(T);
// if (!enumType.IsEnum)
// {
// return dicEnum;
// }
// if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项
// {
// dicEnum.Add(keyDefault, valueDefault);
// }
// string[] fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组
// foreach (var item in fieldstrs)
// {
// string description = string.Empty;
// var field = enumType.GetField(item);
// object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组
// if (arr != null && arr.Length > 0)
// {
// description = ((DescriptionAttribute)arr[0]).Description; //属性描述
// }
// else
// {
// description = item; //描述不存在取字段名称
// }
// dicEnum.Add(description, (int)Enum.Parse(enumType, item)); //不用枚举的value值作为字典key值的原因从枚举例子能看出来,其实这边应该判断他的值不存在,默认取字段名称
// }
// return dicEnum;
//}
/// <summary>
/// 枚举转字典集合,值作为kye,描述(Description特性)作为value)
/// </summary>
/// <typeparam name="T">枚举类名称</typeparam>
/// <param name="keyDefault">默认key值</param>
/// <param name="valueDefault">默认value值</param>
/// <returns>返回生成的字典集合</returns>
public static Dictionary<string, string> EnumListDic<T>(string keyDefault = "", string valueDefault = "")
{
Dictionary<string, string> dicEnum = new Dictionary<string, string>();
Type enumType = typeof(T);
if (!enumType.IsEnum)
{
return dicEnum;
}
//判断是否添加默认选项
if (!string.IsNullOrEmpty(keyDefault))
{
dicEnum.Add(keyDefault, valueDefault);
}
//获取枚举字段数组
string[] names = Enum.GetNames(enumType);
//获取枚举值数组
Array values = Enum.GetValues(enumType);
for (var i = 0; i < names.Length; i++)
{
string description = string.Empty;
var field = enumType.GetField(names[i]);
//获取属性字段数组
object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true);
if (arr != null && arr.Length > 0)
{
//属性描述
description = ((DescriptionAttribute)arr[0]).Description;
}
else
{
//描述不存在取字段名称
description = names[i];
}
string key = Convert.ToInt32(values.GetValue(i)).ToString();
//添加到字典中
dicEnum.Add(key, description);
}
return dicEnum;
}
/// <summary>
/// 将枚举转为List INT的集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<int> EnumListInt<T>()
{
List<int> list = new List<int>();
foreach (var e in Enum.GetNames(typeof(T)))
{
int v = (int)Enum.Parse(typeof(T), e.ToString());
list.Add(v);
}
return list;
}
/// <summary>
/// 将枚举转为List INT的集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<string> EnumListString<T>()
{
List<string> list = new List<string>();
foreach (var e in Enum.GetValues(typeof(T)))
{
string v = ((int)Enum.Parse(typeof(T), e.ToString())).ToString();
list.Add(v);
}
return list;
}
/// <summary>
/// 对象克隆,反射
/// </summary>
/// <typeparam name="TIn"></typeparam>
/// <typeparam name="TOut"></typeparam>
/// <param name="tIn"></param>
/// <returns></returns>
public static TOut TransReflection<TIn, TOut>(TIn tIn)
{
TOut tOut = Activator.CreateInstance<TOut>();
var tInType = tIn.GetType();
foreach (var itemOut in tOut.GetType().GetProperties())
{
var itemIn = tInType.GetProperty(itemOut.Name); ;
if (itemIn != null)
{
try
{
itemOut.SetValue(tOut, itemIn.GetValue(tIn));
}
catch (Exception)
{
}
}
}
return tOut;
}
/// <summary>
/// 获取指定枚举成员的描述
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string ToDescriptionString(this Enum obj)
{
var attribs = (DescriptionAttribute[])obj.GetType().GetField(obj.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
return attribs.Length > 0 ? attribs[0].Description : obj.ToString();
}
}
}