Utility@DWF.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Reflection;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. internal static class _PageExt
  11. {
  12. public static T AddEx<T>(this ControlCollection coll, T ctl) where T : Control
  13. {
  14. coll.Add(ctl);
  15. return ctl;
  16. }
  17. public static T AddEx<T>(this TableCellCollection coll, T ctl) where T : TableCell
  18. {
  19. coll.Add(ctl);
  20. return ctl;
  21. }
  22. }
  23. internal static class _ControlExt
  24. {
  25. public static void VisualText(this TableCell td, string text)
  26. {
  27. if (text == null)
  28. {
  29. td.ForeColor = Color.Red;
  30. td.Text = "NULL";
  31. }
  32. else if (string.IsNullOrEmpty(text))
  33. {
  34. td.ForeColor = Color.Orange;
  35. td.Text = "EMPTY";
  36. }
  37. else
  38. {
  39. td.Text = text;
  40. }
  41. }
  42. }
  43. internal static class _AttributeExt
  44. {
  45. public static string GetDescriptionByAttribute(this ICustomAttributeProvider cap, bool inherit = true, string defaultResult = "")
  46. {
  47. return cap.GetCustomAttributes(inherit)
  48. .OfType<DescriptionAttribute>()
  49. .Select(p => p.Description)
  50. .FirstOrDefault() ?? defaultResult;
  51. }
  52. public static bool HasAttribute<T>(this ICustomAttributeProvider cap, bool inherit = true) where T : Attribute
  53. {
  54. return cap.IsDefined(typeof(T), inherit);
  55. }
  56. }
  57. internal static class TypeParser<T> where T : struct
  58. {
  59. private delegate bool DTryParse(string value, out T result);
  60. private static DTryParse ms_dTryParse;
  61. static TypeParser()
  62. {
  63. Type type = typeof(T);
  64. if (type.Name == "Nullable`1")
  65. throw new NotSupportedException("TypeParser not support Nullable type");
  66. var mTryParse = type
  67. .GetMethods(
  68. BindingFlags.Static
  69. | BindingFlags.Public
  70. | BindingFlags.DeclaredOnly
  71. | BindingFlags.InvokeMethod
  72. ).FirstOrDefault(p =>
  73. p.ReturnType == typeof(bool)
  74. && p.GetParameters().Length == 2
  75. && p.GetParameters()[0].ParameterType == typeof(string)
  76. && p.GetParameters()[1].IsOut
  77. && p.GetParameters()[1].ParameterType.GetElementType() == type
  78. );
  79. if (mTryParse == null)
  80. throw new NotSupportedException("TypeParser can no found `bool TypParse(String,out " + type.Name + ")` metohd");
  81. ms_dTryParse = (DTryParse)Delegate.CreateDelegate(typeof(DTryParse), mTryParse);
  82. }
  83. public static T Convert(object value)
  84. {
  85. if (value == null || value == DBNull.Value)
  86. return default(T);
  87. T result = default(T);
  88. ms_dTryParse(value.ToString(), out result);
  89. return result;
  90. }
  91. public static T? ConvertNullable(object value)
  92. {
  93. if (value == null || value == DBNull.Value)
  94. return null;
  95. T result;
  96. if (ms_dTryParse(value.ToString(), out result))
  97. return result;
  98. return null;
  99. }
  100. }
  101. internal static class _LinqUtility
  102. {
  103. public static T2[] SelectArray<T1, T2>(this IEnumerable<T1> source, Func<T1, T2> selector)
  104. {
  105. return source.Select(selector).ToArray();
  106. }
  107. public static T2[] SelectArray<T1, T2>(this IQueryable<T1> source, Expression<Func<T1, T2>> selector)
  108. {
  109. return source.Select(selector).ToArray();
  110. }
  111. public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> proc)
  112. {
  113. foreach (var item in source)
  114. proc(item);
  115. return source;
  116. }
  117. public static IEnumerable<T> ForEach<T>(this IQueryable<T> source, Action<T> proc)
  118. {
  119. foreach (var item in source)
  120. proc(item);
  121. return source;
  122. }
  123. public static T2 First<T1, T2>(this Dictionary<T1, T2> source)
  124. {
  125. foreach (var item in source)
  126. {
  127. return item.Value;
  128. }
  129. throw new InvalidOperationException("No items in dic");
  130. }
  131. }
  132. internal static class _StringExt
  133. {
  134. public static string ToDisplayString(this object value)
  135. {
  136. if (value is bool)
  137. return (bool)value ? "是" : "否";
  138. if (value is Enum)
  139. {
  140. return value.GetType().GetField(value.ToString()).GetDescriptionByAttribute() ?? value.ToString();
  141. }
  142. if (value == null)
  143. return null;
  144. return value.ToString();
  145. }
  146. public static string JoinString(this IEnumerable<string> source, string separator)
  147. {
  148. return string.Join(separator, source.ToArray());
  149. }
  150. public static string ToHexString(this byte[] buf)
  151. {
  152. return buf.SelectArray(p => p.ToString("X2"))
  153. .JoinString("");
  154. }
  155. public static string Format(this string fmt, params object[] args)
  156. {
  157. return string.Format(fmt, args);
  158. }
  159. }
  160. internal static class _DataExt
  161. {
  162. public static byte[] ToBytes(this int value)
  163. {
  164. return BitConverter.GetBytes(value);
  165. }
  166. }