You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

509 lines
29 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using Apewer;
  2. using Apewer.Internals;
  3. using Apewer.Source;
  4. using Apewer.Web;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Collections.Specialized;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Reflection;
  12. using System.Text;
  13. /// <summary>扩展方法。</summary>
  14. public static class Extensions_Apewer
  15. {
  16. #region Class Utility
  17. /// <summary>克隆对象,创建新对象。可指定包含对象的属性和字段。</summary>
  18. public static T Clone<T>(this T @this, T failed = default(T), bool properties = true, bool fields = false) where T : new() => RuntimeUtility.Clone(@this, failed, properties, fields);
  19. /// <summary>调用 Get 方法。</summary>
  20. public static object Get(this PropertyInfo @this, object instance) => RuntimeUtility.InvokeGet<object>(instance, @this);
  21. /// <summary>调用 Set 方法。</summary>
  22. public static void Set(this PropertyInfo @this, object instance, object value) => RuntimeUtility.InvokeSet<object>(instance, @this, value);
  23. /// <summary>调用 Get 方法。</summary>
  24. public static T Get<T>(this PropertyInfo @this, object instance) => RuntimeUtility.InvokeGet<T>(instance, @this);
  25. /// <summary>调用 Set 方法。</summary>
  26. public static void Set<T>(this PropertyInfo @this, object instance, T value) => RuntimeUtility.InvokeSet<T>(instance, @this, value);
  27. /// <summary>调用方法。</summary>
  28. /// <exception cref="ArgumentException"></exception>
  29. /// <exception cref="InvalidOperationException"></exception>
  30. /// <exception cref="MethodAccessException"></exception>
  31. /// <exception cref="NotSupportedException"></exception>
  32. /// <exception cref="TargetException"></exception>
  33. /// <exception cref="TargetInvocationException"></exception>
  34. /// <exception cref="TargetParameterCountException"></exception>
  35. public static object Invoke(this MethodInfo @this, object instace, params object[] parameters) => RuntimeUtility.InvokeMethod(instace, @this, parameters);
  36. /// <summary>判断静态属性。</summary>
  37. public static bool IsStatic(this PropertyInfo @this) => RuntimeUtility.IsStatic(@this);
  38. /// <summary>以安全的方式获取消息内容,对无效的 Exception 返回 NULL 值。</summary>
  39. public static string Message(this Exception ex) => RuntimeUtility.Message(ex);
  40. #endregion
  41. #region Number
  42. /// <summary>判断此值为零。</summary>
  43. public static bool IsZero(this decimal @this) => @this.Equals(0M);
  44. /// <summary>判断此值为零。</summary>
  45. public static bool IsZero(this double @this) => @this.Equals(0D);
  46. /// <summary>判断此值为零。</summary>
  47. public static bool IsZero(this float @this) => @this.Equals(0F);
  48. /// <summary>判断此值非零。</summary>
  49. public static bool NotZero(this decimal @this) => !@this.Equals(0M);
  50. /// <summary>判断此值非零。</summary>
  51. public static bool NotZero(this double @this) => !@this.Equals(0D);
  52. /// <summary>判断此值非零。</summary>
  53. public static bool NotZero(this float @this) => !@this.Equals(0F);
  54. /// <summary>约束值范围,若源值不在范围中,则修改为接近的值。</summary>
  55. public static T Restrict<T>(this T @this, T min, T max) where T : IComparable => NumberUtility.Restrict(@this, min, max);
  56. #endregion
  57. #region String、StringBuilder
  58. /// <summary>转换为 Boolean 值。</summary>
  59. public static bool Boolean(this object @this) => NumberUtility.Boolean(@this);
  60. /// <summary>转换为 Byte 值。</summary>
  61. public static byte Byte(this object @this) => NumberUtility.Byte(@this);
  62. /// <summary>转换为 Int32 值。</summary>
  63. public static int Int32(this object @this) => NumberUtility.Int32(@this);
  64. /// <summary>转换为 Int64 值。</summary>
  65. public static long Int64(this object @this) => NumberUtility.Int64(@this);
  66. /// <summary>转换为 Decimal 值。</summary>
  67. public static decimal Decimal(this object @this) => NumberUtility.Decimal(@this);
  68. /// <summary>转换为单精度浮点值。</summary>
  69. public static float Float(this object @this) => NumberUtility.Float(@this);
  70. /// <summary>转换为双精度浮点值。</summary>
  71. public static double Double(this object @this) => NumberUtility.Double(@this);
  72. /// <summary>将文本转换为字节数组,默认使用 UTF-8。</summary>
  73. public static byte[] Bytes(this string @this, Encoding encoding = null) => TextUtility.Bytes(@this, encoding);
  74. /// <summary>验证字符串为 NULL、为空或仅含空白。</summary>
  75. public static bool IsEmpty(this string @this) => TextUtility.IsEmpty(@this);
  76. /// <summary>验证字符串为 NULL、为空或仅含空白。</summary>
  77. public static bool NotEmpty(this string @this) => TextUtility.NotEmpty(@this);
  78. /// <summary>验证字符串为 NULL、为空或无实际内容。</summary>
  79. public static bool IsBlank(this string @this) => TextUtility.IsBlank(@this);
  80. /// <summary>验证字符串含有实际内容。</summary>
  81. public static bool NotBlank(this string @this) => TextUtility.NotBlank(@this);
  82. /// <summary>用指定的分隔符拆分文本。</summary>
  83. public static string[] Split(this string @this, string separator) => TextUtility.Split(@this, separator);
  84. /// <summary>使用多个分隔符切分字符串,得到多个子字符串。</summary>
  85. public static string[] Split(this string @this, params char[] separators) => TextUtility.Split(@this, separators);
  86. /// <summary>返回此字符串的安全键副本。</summary>
  87. public static string SafeKey(this string @this, int maxLength = 255) => TextUtility.SafeKey(@this, maxLength);
  88. /// <summary>移除字符串前后的空字符。</summary>
  89. /// <remarks>trimBlank:移除空格、全角空格、换行符、回车符、制表符和换页符。</remarks>
  90. public static string Trim(this string @this, bool trimBlank = false) => TextUtility.Trim(@this, trimBlank);
  91. /// <summary>移除字符串前后的空字符。</summary>
  92. /// <remarks>trimBlank:移除空格、全角空格、换行符、回车符、制表符和换页符。</remarks>
  93. public static string ToTrim(this string @this, bool trimBlank = false) => TextUtility.Trim(@this, trimBlank);
  94. /// <summary>返回此字符串转换为小写形式的副本。</summary>
  95. public static string Lower(this string @this) => TextUtility.Lower(@this);
  96. /// <summary>返回此字符串转换为大写形式的副本。</summary>
  97. public static string Upper(this string @this) => TextUtility.Upper(@this);
  98. /// <summary>追加字符串。</summary>
  99. public static string Append(this string @this, params object[] text) => TextUtility.Merge(@this, TextUtility.Merge(text));
  100. /// <summary>追加文本。</summary>
  101. public static void Append(this StringBuilder @this, params object[] text) => TextUtility.Append(@this, text);
  102. /// <summary>防注入处理,去除会引发代码注入的字符。可限定字符串长度。</summary>
  103. public static string AntiInject(this string @this, int length = -1, string blacklist = null) => TextUtility.AntiInject(@this, length, blacklist);
  104. /// <summary>将 Base64 字符串转换为字节数组。</summary>
  105. public static byte[] Base64(this string @this) => BytesUtility.FromBase64(@this);
  106. /// <summary>对 URL 编码。</summary>
  107. public static string EncodeUrl(this string @this) => UrlEncoding.Encode(@this);
  108. /// <summary>对 URL 解码。</summary>
  109. public static string DecodeUrl(this string @this) => UrlEncoding.Decode(@this);
  110. /// <summary>获取指定长度的字符串片段,可指定 trim 参数对片段再次修剪。</summary>
  111. public static string Left(this string text, int maxLength, bool trim = false) => TextUtility.Left(text, maxLength, trim);
  112. /// <summary>获取指定长度的字符串片段,可指定 trim 参数对片段再次修剪。</summary>
  113. public static string Right(this string text, int maxLength, bool trim = false) => TextUtility.Right(text, maxLength, trim);
  114. /// <summary>剪取文本内容,若指定头部为空则从原文本首部起,若指定尾部为空则至原文本末尾。</summary>
  115. /// <returns>剪取后的内容,不包含 head 和 foot。</returns>
  116. public static string Cut(this string text, string head = null, string foot = null) => TextUtility.Cut(text, head, foot);
  117. /// <summary>约束字符串中的字符,只包含指定的字符。</summary>
  118. public static string Restrict(this string text, char[] chars) => TextUtility.Restrict(text, chars);
  119. /// <summary>约束字符串中的字符,只包含指定的字符。</summary>
  120. public static string Restrict(this string text, string chars) => TextUtility.Restrict(text, chars);
  121. #endregion
  122. #region Byte[]
  123. /// <summary>将字节数组格式化为十六进制字符串,默认为大写。<para>例:D41D8CD98F00B204E9800998ECF8427E</para></summary>
  124. public static string X2(this byte[] @this, bool upperCase = true) => BytesUtility.ToX2(upperCase, @this);
  125. /// <summary>克隆字节数组。当源为 NULL 时,将获取零元素字节数组。</summary>
  126. public static byte[] Clone(this byte[] @this) => BytesUtility.Clone(@this);
  127. /// <summary>确定此字节数组实例的开头是否与指定的字节数组匹配。</summary>
  128. public static bool Starts(this byte[] @this, params byte[] head) => BytesUtility.StartsWith(@this, head);
  129. /// <summary>确定此字节数组实例的结尾是否与指定的字节数组匹配。</summary>
  130. public static bool Ends(this byte[] @this, params byte[] head) => BytesUtility.EndsWith(@this, head);
  131. /// <summary>将字节数组转换为 Base64 字符串。</summary>
  132. public static string Base64(this byte[] @this) => BytesUtility.ToBase64(@this);
  133. /// <summary>获取 MD5 值。</summary>
  134. public static byte[] MD5(params byte[] @this) => BytesUtility.MD5(@this);
  135. /// <summary>获取 SHA1 值。</summary>
  136. public static byte[] SHA1(params byte[] @this) => BytesUtility.SHA1(@this);
  137. /// <summary>获取 SHA256 值。</summary>
  138. public static byte[] SHA256(params byte[] @this) => BytesUtility.SHA256(@this);
  139. /// <summary>将字节数组转换为文本,默认为 UTF-8。</summary>
  140. public static string Text(this byte[] @this, Encoding encoding = null) => TextUtility.FromBytes(@this, encoding);
  141. /// <summary>为字节数组增加字节。</summary>
  142. public static byte[] Append(this byte[] @this, params byte[] bytes) => BytesUtility.Merge(@this, bytes);
  143. /// <summary>检查字节数组是 UTF-8 文本,默认最多检测 1MB 数据(指定为 0 将不限制检查长度)。</summary>
  144. public static bool IsUTF8(this byte[] @this, int checkLength = 1048576) => TextUtility.IsUTF8(@this, checkLength, null);
  145. /// <summary>检查字节数组包含 UTF-8 BOM 头。</summary>
  146. public static bool ContainsBOM(this byte[] @this) => TextUtility.ContainsBOM(@this);
  147. #endregion
  148. #region DateTime
  149. /// <summary>获取毫秒时间戳。</summary>
  150. public static long Stamp(this DateTime @this, bool byMilliseconds = true) => ClockUtility.Stamp(@this, byMilliseconds);
  151. /// <summary>转换为易于阅读的文本。</summary>
  152. /// <remarks>格式:1970-01-01 00:00:00.000</remarks>
  153. public static string Lucid(this DateTime @this, bool date = true, bool time = true, bool seconds = true, bool milliseconds = true) => ClockUtility.Lucid(@this, date, time, seconds, milliseconds);
  154. /// <summary>转换为紧凑的文本。</summary>
  155. public static string Compact(this DateTime @this, bool date = true, bool time = true, bool seconds = true, bool milliseconds = true) => ClockUtility.Compact(@this, date, time, seconds, milliseconds);
  156. /// <summary>当前 DateTime 为闰年。</summary>
  157. public static bool LeapYear(this DateTime @this) => ClockUtility.IsLeapYear(@this);
  158. /// <summary>从毫秒时间戳获取 DateTime 对象。发生异常且不允许异常时将返回 1970-01-01 00:00:00.000。</summary>
  159. /// <exception cref="ArgumentOutOfRangeException"></exception>
  160. public static DateTime DateTime(this long stamp, bool throwException = true) => ClockUtility.FromStamp(stamp, throwException);
  161. #endregion
  162. #region Json
  163. /// <summary>Json 对象可用。</summary>
  164. public static bool Available(this Json @this) => @this != null && @this.Available;
  165. /// <summary>生成 JSON 对象,失败时返回 NULL。</summary>
  166. /// <param name="this">将要解析的对象。</param>
  167. /// <param name="lowerCase">在 Json 中将属性名称转换为小写。</param>
  168. /// <param name="depth">解析深度。</param>
  169. /// <param name="force">强制解析所有属性,忽略 Serializable 特性。</param>
  170. public static Json Json(this IDictionary @this, bool lowerCase = false, int depth = -1, bool force = false) => Apewer.Json.From(@this, lowerCase, depth, force);
  171. /// <summary>解析实现 IList 的对象为 Json 对象,失败时返回 Null。</summary>
  172. /// <param name="this">将要解析的对象。</param>
  173. /// <param name="lowerCase">在 Json 中将属性名称转换为小写。</param>
  174. /// <param name="depth">解析深度。</param>
  175. /// <param name="force">强制解析所有属性,忽略 Serializable 特性。</param>
  176. public static Json Json(this IList @this, bool lowerCase = false, int depth = -1, bool force = false) => Apewer.Json.From(@this, lowerCase, depth, force);
  177. /// <summary>
  178. /// <para>解析对象为 Json 对象,包含所有公共属性,失败时返回 Null。</para>
  179. /// <para>String 对象将解析文本;Json 对象将返回实例;String 对象将解析文本;实现 IDictionary 或 IList 的对象将解析内容。</para>
  180. /// </summary>
  181. /// <param name="this">将要解析的对象。</param>
  182. /// <param name="lowerCase">在 Json 中将属性名称转换为小写。</param>
  183. /// <param name="depth">解析深度。</param>
  184. /// <param name="force">强制解析所有属性,忽略 Serializable 特性。</param>
  185. /// <exception cref="System.Exception"></exception>
  186. public static Json Json(object @this, bool lowerCase = false, int depth = -1, bool force = false) => Apewer.Json.From(@this, lowerCase, depth, force);
  187. /// <summary>填充类型实例,失败时返回 NULL 值。</summary>
  188. /// <param name="this">将要解析的对象。</param>
  189. /// <param name="ignoreCase">忽略属性名称大小写。</param>
  190. /// <param name="ignoreChars">忽略的属性名称字符。</param>
  191. /// <param name="force">强制填充,忽略 <typeparamref name="T"/> 的 Serializable 特性。</param>
  192. public static T Object<T>(this Json @this, bool ignoreCase = true, string ignoreChars = null, bool force = false) where T : class, new() => Apewer.Json.Object<T>(@this, ignoreCase, ignoreChars, force);
  193. /// <summary>填充类型实例,失败时返回 NULL 值。</summary>
  194. /// <param name="this">将要解析的对象。</param>
  195. /// <param name="ignoreCase">忽略属性名称大小写。</param>
  196. /// <param name="ignoreChars">忽略的属性名称字符。</param>
  197. /// <param name="force">强制填充,忽略 <typeparamref name="T"/> 的 Serializable 特性。</param>
  198. public static T[] Array<T>(this Json @this, bool ignoreCase = true, string ignoreChars = null, bool force = false) where T : class, new() => Apewer.Json.Array<T>(@this, ignoreCase, ignoreChars, force);
  199. /// <summary>设置属性名称为小写。</summary>
  200. public static Json Lower(this Json @this) => Apewer.Json.Lower(@this);
  201. /// <summary>设置属性名称为驼峰形式。</summary>
  202. public static Json Camel(this Json @this) => Apewer.Json.Camel(@this);
  203. #endregion
  204. #region Logger
  205. /// <summary>记录异常。</summary>
  206. public static void Exception(this Logger logger, object sender, Exception exception) => logger?.InnerException(sender, exception);
  207. /// <summary>记录错误。多个 Content 参数将以“ | ”分隔。</summary>
  208. public static void Error(this Logger logger, object sender, params object[] content) => logger?.InnerError(sender, content);
  209. /// <summary>记录警告。多个 Content 参数将以“ | ”分隔。</summary>
  210. public static void Warning(this Logger logger, object sender, params object[] content) => logger?.InnerWarning(sender, content);
  211. /// <summary>记录警告。多个 Content 参数将以“ | ”分隔。</summary>
  212. public static void Info(this Logger logger, object sender, params object[] content) => logger?.InnerInfo(sender, content);
  213. /// <summary>记录文本。多个 Content 参数将以“ | ”分隔。</summary>
  214. public static void Text(this Logger logger, object sender, params object[] content) => logger?.InnerText(sender, content);
  215. /// <summary>记录调试。多个 Content 参数将以“ | ”分隔。</summary>
  216. [Conditional("DEBUG")]
  217. public static void Debug(this Logger logger, object sender, params object[] content) => logger?.InnerDebug(sender, content);
  218. #endregion
  219. #region Stream
  220. /// <summary>重置流的位置到开始位置。</summary>
  221. public static bool ResetPosition(this Stream @this) => BytesUtility.ResetPosition(@this);
  222. /// <summary>读取源流中的数据,并将数据写入目标流,获取写入的字节数。</summary>
  223. public static long Read(this Stream @this, Stream destination, Action<long> progress = null) => BytesUtility.Read(@this, destination, progress);
  224. /// <summary>读取源流中的数据,并将数据写入目标流,获取写入的字节数。</summary>
  225. public static long Read(this Stream @this, Stream destination, Func<long, bool> progress = null) => BytesUtility.Read(@this, destination, progress);
  226. /// <summary>读取源流中的数据,获取写入的字节。</summary>
  227. public static byte[] Read(this Stream @this, bool dispose) => BytesUtility.Read(@this, 1024, dispose);
  228. /// <summary>读取源流中的数据。</summary>
  229. public static byte[] Read(this Stream @this, int buffer = 1024, bool dispose = false) => BytesUtility.Read(@this, buffer, dispose);
  230. /// <summary>向流写入数据。</summary>
  231. /// <param name="this">源流。</param>
  232. /// <param name="bytes">要写入的数据。</param>
  233. public static long Write(this Stream @this, params byte[] bytes) => BytesUtility.Write(@this, bytes);
  234. /// <summary>读取源流中的数据,并将数据写入当前流,获取写入的字节数。</summary>
  235. /// <param name="this">目标流。</param>
  236. /// <param name="source">源流。</param>
  237. public static long Write(this Stream @this, Stream source) => BytesUtility.Read(source, @this);
  238. /// <summary>获取 MD5 值。</summary>
  239. public static byte[] MD5(this Stream @this) => BytesUtility.MD5(@this);
  240. /// <summary>获取 SHA1 值。</summary>
  241. public static byte[] SHA1(this Stream @this) => BytesUtility.SHA1(@this);
  242. #if !NET20
  243. /// <summary>获取 SHA256 值。</summary>
  244. public static byte[] SHA256(this Stream @this) => BytesUtility.SHA256(@this);
  245. #endif
  246. #endregion
  247. #region Rrflection
  248. /// <summary>判断指定类型具有特性。</summary>
  249. public static bool Contains<T>(this ICustomAttributeProvider @this, bool inherit = false) where T : Attribute => RuntimeUtility.Contains<T>(@this, inherit);
  250. #endregion
  251. #region Collection
  252. /// <summary>添加多个元素。</summary>
  253. public static void Add<T>(this List<T> @this, params T[] items)
  254. {
  255. if (@this != null && items != null) @this.AddRange(items);
  256. }
  257. /// <summary>添加元素。</summary>
  258. public static bool Add<TKey, TValue>(this IList<KeyValuePair<TKey, TValue>> @this, TKey key, TValue value) => CollectionHelper.Add<TKey, TValue>(@this, key, value);
  259. /// <summary>判断集合为空。</summary>
  260. public static bool IsEmpty<T>(this IEnumerable<T> @this) => CollectionHelper.IsEmpty(@this);
  261. /// <summary></summary>
  262. public static bool NotEmpty<T>(this IEnumerable<T> @this) => CollectionHelper.NotEmpty(@this);
  263. /// <summary></summary>
  264. public static bool Contains<T>(this IEnumerable<T> @this, T cell) => CollectionHelper.Contains(@this, cell);
  265. /// <summary>获取集合中元素的数量。</summary>
  266. public static int Count<T>(this IEnumerable<T> @this) => CollectionHelper.Count(@this);
  267. /// <summary>对元素去重,且去除 NULL 值。</summary>
  268. public static T[] Distinct<T>(this IEnumerable<T> @this) => CollectionHelper.Distinct(@this);
  269. /// <summary>获取可枚举集合的部分元素。</summary>
  270. public static T[] Slice<T>(this IEnumerable<T> @this, int start = 0, int count = -1, Func<T> stuffer = null) => CollectionHelper.Slice<T>(@this, start, count, stuffer);
  271. /// <summary>安全转换为 List&lt;<typeparamref name="T"/>&gt; 对象。可指定排除 NULL 值元素。</summary>
  272. /// <typeparam name="T"></typeparam>
  273. public static List<T> List<T>(this IEnumerable<T> @this, bool excludeNull = false) => CollectionHelper.ToList<T>(@this, excludeNull);
  274. /// <summary>安全转换为 &lt;<typeparamref name="T"/>&gt;[] 对象。可指定排除 NULL 值元素。</summary>
  275. /// <typeparam name="T"></typeparam>
  276. public static T[] Array<T>(IEnumerable<T> @this, bool excludeNull = false) => CollectionHelper.ToArray<T>(@this, excludeNull);
  277. /// <summary>对列表中的元素排序。</summary>
  278. public static List<T> Sort<T>(this List<T> @this, Func<T, T, int> comparison) => CollectionHelper.Sort(@this, comparison);
  279. /// <summary>对字典中的键排序。</summary>
  280. public static Dictionary<TKey, TValue> SortKey<TKey, TValue>(this Dictionary<TKey, TValue> @this, Func<TKey, TKey, int> comparison) => CollectionHelper.SortKey(@this, comparison);
  281. /// <summary>对字典中的键排序。</summary>
  282. public static Dictionary<TKey, TValue> SortKey<TKey, TValue>(this Dictionary<TKey, TValue> @this) where TKey : IComparable<TKey> => CollectionHelper.SortKey(@this, (a, b) => a.CompareTo(b));
  283. /// <summary>对字典中的值排序。</summary>
  284. public static Dictionary<TKey, TValue> SortValue<TKey, TValue>(this Dictionary<TKey, TValue> @this, Func<TValue, TValue, int> comparison) => CollectionHelper.SortValue(@this, comparison);
  285. /// <summary>对字典中的值排序。</summary>
  286. public static Dictionary<TKey, TValue> SortValue<TKey, TValue>(this Dictionary<TKey, TValue> @this) where TValue : IComparable<TValue> => CollectionHelper.SortValue(@this, (a, b) => a.CompareTo(b));
  287. /// <summary>获取集合中的第一个元素。可指定失败时的默认返回值。</summary>
  288. public static T First<T>(this IEnumerable<T> collection, T failed = default(T)) => CollectionHelper.First(collection, failed);
  289. /// <summary>获取集合中的最后一个元素。可指定失败时的默认返回值。</summary>
  290. public static T Last<T>(this IEnumerable<T> collection, T failed = default(T)) => CollectionHelper.Last(collection, failed);
  291. /// <summary>生成 StringPairs 对象实例为副本。</summary>
  292. public static StringPairs StringPairs(this NameValueCollection @this) => Apewer.StringPairs.From(@this);
  293. /// <summary>解析源对象。</summary>
  294. public static Dictionary<string, string> Origin(this TextSet instance) => instance == null ? null : instance.Origin;
  295. /// <summary>解析源对象。</summary>
  296. public static Dictionary<string, object> Origin(this ObjectSet instance) => instance == null ? null : instance.Origin;
  297. /// <summary>解析源对象。</summary>
  298. public static Dictionary<string, T> Origin<T>(this ObjectSet<T> instance) => instance == null ? null : instance.Origin;
  299. #endregion
  300. #region Source
  301. /// <summary>获取默认表中指定单元格的内容。从第 0 行第 0 列开始。</summary>
  302. public static Class<DateTime> DateTime(this IQuery @this, int row = 0, int column = 0) => @this == null ? null : Query.DateTime(@this.Value(row, column));
  303. /// <summary>获取默认表中指定单元格的内容。从第 0 行开始。</summary>
  304. public static Class<DateTime> DateTime(this IQuery @this, int row, string column) => @this == null ? null : Query.DateTime(@this.Value(row, column));
  305. /// <summary>获取默认表中指定单元格的内容。从第 0 行第 0 列开始。</summary>
  306. public static Int32 Int32(this IQuery @this, int row = 0, int column = 0) => @this == null ? 0 : Int32(@this.Text(row, column));
  307. /// <summary>获取默认表中指定单元格的内容。从第 0 行开始。</summary>
  308. public static Int32 Int32(this IQuery @this, int row, string column) => @this == null ? 0 : Int32(@this.Text(row, column));
  309. /// <summary>获取默认表中指定单元格的内容。从第 0 行第 0 列开始。</summary>
  310. public static Int64 Int64(this IQuery @this, int row = 0, int column = 0) => @this == null ? 0L : Int64(@this.Text(row, column));
  311. /// <summary>获取默认表中指定单元格的内容。从第 0 行开始。</summary>
  312. public static Int64 Int64(this IQuery @this, int row, string column) => @this == null ? 0L : Int64(@this.Text(row, column));
  313. /// <summary>获取默认表中指定单元格的内容。从第 0 行第 0 列开始。</summary>
  314. public static Decimal Decimal(this IQuery @this, int row = 0, int column = 0) => @this == null ? 0M : Decimal(@this.Text(row, column));
  315. /// <summary>获取默认表中指定单元格的内容。从第 0 行开始。</summary>
  316. public static Decimal Decimal(this IQuery @this, int row, string column) => @this == null ? 0M : Decimal(@this.Text(row, column));
  317. /// <summary>获取默认表中指定单元格的内容。从第 0 行第 0 列开始。</summary>>
  318. public static Double Double(this IQuery @this, int row = 0, int column = 0) => @this == null ? 0D : Double(@this.Text(row, column));
  319. /// <summary>获取默认表中指定单元格的内容。从第 0 行开始。</summary>>
  320. public static Double Double(this IQuery @this, int row, string column) => @this == null ? 0D : Double(@this.Text(row, column));
  321. /// <summary>获取默认表中指定单元格的内容。从第 0 行第 0 列开始。</summary>
  322. public static string Text(this IQuery @this, int row = 0, int column = 0) => @this == null ? null : Query.Text(@this.Value(row, column));
  323. /// <summary>获取默认表中指定单元格的内容。从第 0 行开始。</summary>
  324. public static string Text(this IQuery @this, int row, string column) => @this == null ? null : Query.Text(@this.Value(row, column));
  325. #endregion
  326. #region Web
  327. /// <summary>获取参数,指定可能的参数名,默认将修剪参数值。</summary>
  328. /// <remarks>当从 URL 中获取参数时将解码。</remarks>
  329. public static string Parameter(this ApiRequest @this, params string[] names) => ApiUtility.Parameter(@this, true, names);
  330. /// <summary>设置 status 为 error,并设置 message 的内容。</summary>
  331. public static void Error(this ApiResponse @this, string message = "未知错误。") => ApiUtility.Error(@this, message);
  332. /// <summary>输出字节数组。</summary>
  333. public static void Bytes(this ApiResponse @this, byte[] bytes, string type = "application/octet-stream") => ApiUtility.Model(@this, type, new ApiBytesModel() { Bytes = bytes });
  334. /// <summary>输出 UTF-8 文本。</summary>
  335. public static void Text(this ApiResponse @this, string text, string contentType = "text/plain; charset=utf-8") => ApiUtility.Model(@this, null, new ApiTextModel() { Text = text, ContentType = contentType });
  336. /// <summary>输出 Json 文本。</summary>
  337. public static void Json(this ApiResponse @this, Json json, bool indented = true, bool camel = false) => ApiUtility.Model(@this, null, new ApiJsonModel() { Json = json, Indented = indented, Camel = camel });
  338. /// <summary>输出文件。</summary>
  339. public static void File(this ApiResponse @this, string path, string name = null) => ApiUtility.Model(@this, null, new ApiFileModel() { Path = path, Attachment = name });
  340. /// <summary>重定向。</summary>
  341. public static void Redirect(this ApiResponse @this, string location) => ApiUtility.Model(@this, null, new ApiRedirectModel() { Location = location });
  342. /// <summary>设置响应,当发生错误时设置响应。返回错误信息。</summary>
  343. public static string Set(this ApiResponse @this, IList list, bool lower = true, int depth = -1, bool force = false) => ApiUtility.Respond(@this, list, lower, depth, force);
  344. /// <summary>设置响应,当发生错误时设置响应。返回错误信息。</summary>
  345. public static string Set(this ApiResponse @this, IRecord record, bool lower = true) => ApiUtility.Respond(@this, record, lower);
  346. /// <summary>设置响应,当发生错误时设置响应。返回错误信息。</summary>
  347. public static string Set(this ApiResponse @this, Json data, bool lower = true) => ApiUtility.Respond(@this, data, lower);
  348. #endregion
  349. }