Extends.cs
6.98 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using RCS.Model;
using RCS.Model.Comm;
using RCS.Model.Entity;
using System.ComponentModel;
using static RCS.Model.Comm.EnumMsg;
namespace RCS.WinClient.Common
{
public static class Extends
{
/// <summary>
/// 获取点的Point属性
/// </summary>
/// <param name="barcode"></param>
/// <returns></returns>
public static BllResult<Base_Point> Point(this string barcode)
{
var point = App.PointList.Find(a => a.Barcode == barcode);
if (point == null)
{
return BllResult<Base_Point>.Error();
}
return BllResult<Base_Point>.Success(point);
}
/// <summary>
/// 获取区间值
/// </summary>
/// <param name="value">被比较的X轴或是Y轴</param>
/// <param name="doublieX">当前点的Y轴</param>
/// <param name="doubleY">当前点的X轴</param>
/// <param name="flag">1比较X轴,2比较Y轴</param>
/// <returns></returns>
public static bool intBetween(this int value, double doublieX, double doubleY, int flag)
{
if (flag == 1)
{
if (doublieX > -2600 && doublieX < -2500)
{
return value < doublieX + 5 && value > doublieX - 5;
}
else if (doublieX > -10200 && doublieX < -8729 && doubleY < 500 && doubleY > 300)
{
return value < doublieX + 6 && value > doublieX - 6;
}
else if (doublieX > 1800 && doublieX < 1970)
{
return value < doublieX + 10 && value > doublieX - 10;
}
return value < doublieX + 25 && value > doublieX - 25;
}
return value < doubleY + 25 && value > doubleY - 25;
}
/// <summary>
/// 获取角度区间值
/// </summary>
/// <param name="value"></param>
/// <param name="num"></param>
/// <returns></returns>
public static bool DLAgvOriBetween(this float value, float num)
{
return value < num + 60 && value > num - 60;
}
/// <summary>
/// 获取枚举成员的值(this是扩展方法的标志)
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static int GetIndexInt(this Enum obj)
{
return Convert.ToInt32(obj);
}
/// <summary>
/// 获取枚举成员的值(this是扩展方法的标志)
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string GetIndexString(this Enum obj)
{
return Convert.ToInt32(obj).ToString();
}
public static T ToEnum<T>(this string obj) where T : struct
{
if (string.IsNullOrEmpty(obj))
{
return default(T);
}
try
{
return (T)Enum.Parse(typeof(T), obj, true);
}
catch (Exception)
{
return default(T);
}
}
/// <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();
}
/// <summary>
/// 根据枚举值,获取指定枚举类的成员描述
/// </summary>
/// <param name="type"></param>
/// <param name="id"></param>
/// <returns></returns>
public static string GetDescriptionString(this Type type, int? id)
{
var values = from Enum e in Enum.GetValues(type)
select new { id = e.GetIndexInt(), name = e.ToDescriptionString() };
if (!id.HasValue) id = 0;
try
{
return values.ToList().Find(c => c.id == id).name;
}
catch
{
return "";
}
}
public static string GetDateString(this DateTime? dateTime)
{
if (dateTime == null)
{
return "null";
}
else
{
return dateTime.ToString();
}
}
public static DateTime? GetDateTime(this object str)
{
if (string.IsNullOrWhiteSpace(str.ToString()))
{
return null;
}
else
{
return DateTime.Parse(str.ToString());
}
}
public static List<T> EnumToList<T>() where T : Enum
{
return Enum.GetValues(typeof(T)).Cast<T>().ToList();
}
/// <summary>
/// 根据索引获取列表数据,当列表为空时,返回默认值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="index"></param>
/// <returns></returns>
public static T? Get<T>(this IList<T> list, int index)
{
T? item = ((list?.Count ?? 0) - 1 < index || list!.Count < index) ? default : list![index];
return item;
}
public static bool IsNullOrEmpty(this string? str)
{
return string.IsNullOrEmpty(str);
}
public static bool In<T>(this T obj, params T[] arg)
{
for (int i = 0; i < arg.Length; i++)
{
if (arg[i] == null)
{
if (obj == null)
return true;
continue;
}
else if (arg[i]!.Equals(obj))
{
return true;
}
}
return false;
}
public static bool IntBetween(this int value, double value2, int error = 25)
{
return Math.Abs(value2 - value) < error;
}
}
public class NullFormat : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type service)
{
if (service == typeof(ICustomFormatter))
{
return this;
}
else
{
return null;
}
}
public string Format(string format, object arg, IFormatProvider provider)
{
if (arg == null)
{
return "null";
}
IFormattable formattable = arg as IFormattable;
if (formattable != null)
{
return formattable.ToString(format, provider);
}
return arg.ToString();
}
}
}