using Apewer;
using Apewer.Internals;
using Apewer.Source;
using Apewer.Web;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
/// 扩展方法。
public static class Extensions_Apewer
{
#region Class Utility
/// 克隆对象,创建新对象。可指定包含对象的属性和字段。
public static T Clone(this T @this, T failed = default(T), bool properties = true, bool fields = false) where T : new() => RuntimeUtility.Clone(@this, failed, properties, fields);
/// 调用 Get 方法。
public static object Get(this PropertyInfo @this, object instance) => RuntimeUtility.InvokeGet(instance, @this);
/// 调用 Set 方法。
public static void Set(this PropertyInfo @this, object instance, object value) => RuntimeUtility.InvokeSet(instance, @this, value);
/// 调用 Get 方法。
public static T Get(this PropertyInfo @this, object instance) => RuntimeUtility.InvokeGet(instance, @this);
/// 调用 Set 方法。
public static void Set(this PropertyInfo @this, object instance, T value) => RuntimeUtility.InvokeSet(instance, @this, value);
/// 调用方法。
///
///
///
///
///
///
///
public static object Invoke(this MethodInfo @this, object instace, params object[] parameters) => RuntimeUtility.InvokeMethod(instace, @this, parameters);
/// 判断静态属性。
public static bool IsStatic(this PropertyInfo @this) => RuntimeUtility.IsStatic(@this);
/// 以安全的方式获取消息内容,对无效的 Exception 返回 NULL 值。
public static string Message(this Exception ex) => RuntimeUtility.Message(ex);
#endregion
#region Number
/// 判断此值为零。
public static bool IsZero(this decimal @this) => @this.Equals(0M);
/// 判断此值为零。
public static bool IsZero(this double @this) => @this.Equals(0D);
/// 判断此值为零。
public static bool IsZero(this float @this) => @this.Equals(0F);
/// 判断此值非零。
public static bool NotZero(this decimal @this) => !@this.Equals(0M);
/// 判断此值非零。
public static bool NotZero(this double @this) => !@this.Equals(0D);
/// 判断此值非零。
public static bool NotZero(this float @this) => !@this.Equals(0F);
/// 约束值范围,若源值不在范围中,则修改为接近的值。
public static T Restrict(this T @this, T min, T max) where T : IComparable => NumberUtility.Restrict(@this, min, max);
#endregion
#region String、StringBuilder
/// 转换为 Boolean 值。
public static bool Boolean(this object @this) => NumberUtility.Boolean(@this);
/// 转换为 Byte 值。
public static byte Byte(this object @this) => NumberUtility.Byte(@this);
/// 转换为 Int32 值。
public static int Int32(this object @this) => NumberUtility.Int32(@this);
/// 转换为 Int64 值。
public static long Int64(this object @this) => NumberUtility.Int64(@this);
/// 转换为 Decimal 值。
public static decimal Decimal(this object @this) => NumberUtility.Decimal(@this);
/// 转换为单精度浮点值。
public static float Float(this object @this) => NumberUtility.Float(@this);
/// 转换为双精度浮点值。
public static double Double(this object @this) => NumberUtility.Double(@this);
/// 将文本转换为字节数组,默认使用 UTF-8。
public static byte[] Bytes(this string @this, Encoding encoding = null) => TextUtility.Bytes(@this, encoding);
/// 验证字符串为 NULL、为空或仅含空白。
public static bool IsEmpty(this string @this) => TextUtility.IsEmpty(@this);
/// 验证字符串为 NULL、为空或仅含空白。
public static bool NotEmpty(this string @this) => TextUtility.NotEmpty(@this);
/// 验证字符串为 NULL、为空或无实际内容。
public static bool IsBlank(this string @this) => TextUtility.IsBlank(@this);
/// 验证字符串含有实际内容。
public static bool NotBlank(this string @this) => TextUtility.NotBlank(@this);
/// 用指定的分隔符拆分文本。
public static string[] Split(this string @this, string separator) => TextUtility.Split(@this, separator);
/// 使用多个分隔符切分字符串,得到多个子字符串。
public static string[] Split(this string @this, params char[] separators) => TextUtility.Split(@this, separators);
/// 返回此字符串的安全键副本。
public static string SafeKey(this string @this, int maxLength = 255) => TextUtility.SafeKey(@this, maxLength);
/// 移除字符串前后的空字符。
/// trimBlank:移除空格、全角空格、换行符、回车符、制表符和换页符。
public static string Trim(this string @this, bool trimBlank = false) => TextUtility.Trim(@this, trimBlank);
/// 移除字符串前后的空字符。
/// trimBlank:移除空格、全角空格、换行符、回车符、制表符和换页符。
public static string ToTrim(this string @this, bool trimBlank = false) => TextUtility.Trim(@this, trimBlank);
/// 返回此字符串转换为小写形式的副本。
public static string Lower(this string @this) => TextUtility.Lower(@this);
/// 返回此字符串转换为大写形式的副本。
public static string Upper(this string @this) => TextUtility.Upper(@this);
/// 追加字符串。
public static string Append(this string @this, params object[] text) => TextUtility.Merge(@this, TextUtility.Merge(text));
/// 追加文本。
public static void Append(this StringBuilder @this, params object[] text) => TextUtility.Append(@this, text);
/// 防注入处理,去除会引发代码注入的字符。可限定字符串长度。
public static string AntiInject(this string @this, int length = -1, string blacklist = null) => TextUtility.AntiInject(@this, length, blacklist);
/// 将 Base64 字符串转换为字节数组。
public static byte[] Base64(this string @this) => BytesUtility.FromBase64(@this);
/// 对 URL 编码。
public static string EncodeUrl(this string @this) => UrlEncoding.Encode(@this);
/// 对 URL 解码。
public static string DecodeUrl(this string @this) => UrlEncoding.Decode(@this);
/// 获取指定长度的字符串片段,可指定 trim 参数对片段再次修剪。
public static string Left(this string text, int maxLength, bool trim = false) => TextUtility.Left(text, maxLength, trim);
/// 获取指定长度的字符串片段,可指定 trim 参数对片段再次修剪。
public static string Right(this string text, int maxLength, bool trim = false) => TextUtility.Right(text, maxLength, trim);
/// 剪取文本内容,若指定头部为空则从原文本首部起,若指定尾部为空则至原文本末尾。
/// 剪取后的内容,不包含 head 和 foot。
public static string Cut(this string text, string head = null, string foot = null) => TextUtility.Cut(text, head, foot);
/// 约束字符串中的字符,只包含指定的字符。
public static string Restrict(this string text, char[] chars) => TextUtility.Restrict(text, chars);
/// 约束字符串中的字符,只包含指定的字符。
public static string Restrict(this string text, string chars) => TextUtility.Restrict(text, chars);
#endregion
#region Byte[]
/// 将字节数组格式化为十六进制字符串,默认为大写。例:D41D8CD98F00B204E9800998ECF8427E
public static string X2(this byte[] @this, bool upperCase = true) => BytesUtility.ToX2(upperCase, @this);
/// 克隆字节数组。当源为 NULL 时,将获取零元素字节数组。
public static byte[] Clone(this byte[] @this) => BytesUtility.Clone(@this);
/// 确定此字节数组实例的开头是否与指定的字节数组匹配。
public static bool Starts(this byte[] @this, params byte[] head) => BytesUtility.StartsWith(@this, head);
/// 确定此字节数组实例的结尾是否与指定的字节数组匹配。
public static bool Ends(this byte[] @this, params byte[] head) => BytesUtility.EndsWith(@this, head);
/// 将字节数组转换为 Base64 字符串。
public static string Base64(this byte[] @this) => BytesUtility.ToBase64(@this);
/// 获取 MD5 值。
public static byte[] MD5(params byte[] @this) => BytesUtility.MD5(@this);
/// 获取 SHA1 值。
public static byte[] SHA1(params byte[] @this) => BytesUtility.SHA1(@this);
/// 获取 SHA256 值。
public static byte[] SHA256(params byte[] @this) => BytesUtility.SHA256(@this);
/// 将字节数组转换为文本,默认为 UTF-8。
public static string Text(this byte[] @this, Encoding encoding = null) => TextUtility.FromBytes(@this, encoding);
/// 为字节数组增加字节。
public static byte[] Append(this byte[] @this, params byte[] bytes) => BytesUtility.Merge(@this, bytes);
/// 检查字节数组是 UTF-8 文本,默认最多检测 1MB 数据(指定为 0 将不限制检查长度)。
public static bool IsUTF8(this byte[] @this, int checkLength = 1048576) => TextUtility.IsUTF8(@this, checkLength, null);
/// 检查字节数组包含 UTF-8 BOM 头。
public static bool ContainsBOM(this byte[] @this) => TextUtility.ContainsBOM(@this);
#endregion
#region DateTime
/// 获取毫秒时间戳。
public static long Stamp(this DateTime @this, bool byMilliseconds = true) => ClockUtility.Stamp(@this, byMilliseconds);
/// 转换为易于阅读的文本。
/// 格式:1970-01-01 00:00:00.000
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);
/// 转换为紧凑的文本。
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);
/// 当前 DateTime 为闰年。
public static bool LeapYear(this DateTime @this) => ClockUtility.IsLeapYear(@this);
/// 从毫秒时间戳获取 DateTime 对象。发生异常且不允许异常时将返回 1970-01-01 00:00:00.000。
///
public static DateTime DateTime(this long stamp, bool throwException = true) => ClockUtility.FromStamp(stamp, throwException);
#endregion
#region Json
/// Json 对象可用。
public static bool Available(this Json @this) => @this != null && @this.Available;
/// 生成 JSON 对象,失败时返回 NULL。
/// 将要解析的对象。
/// 在 Json 中将属性名称转换为小写。
/// 解析深度。
/// 强制解析所有属性,忽略 Serializable 特性。
public static Json Json(this IDictionary @this, bool lowerCase = false, int depth = -1, bool force = false) => Apewer.Json.From(@this, lowerCase, depth, force);
/// 解析实现 IList 的对象为 Json 对象,失败时返回 Null。
/// 将要解析的对象。
/// 在 Json 中将属性名称转换为小写。
/// 解析深度。
/// 强制解析所有属性,忽略 Serializable 特性。
public static Json Json(this IList @this, bool lowerCase = false, int depth = -1, bool force = false) => Apewer.Json.From(@this, lowerCase, depth, force);
///
/// 解析对象为 Json 对象,包含所有公共属性,失败时返回 Null。
/// String 对象将解析文本;Json 对象将返回实例;String 对象将解析文本;实现 IDictionary 或 IList 的对象将解析内容。
///
/// 将要解析的对象。
/// 在 Json 中将属性名称转换为小写。
/// 解析深度。
/// 强制解析所有属性,忽略 Serializable 特性。
///
public static Json Json(object @this, bool lowerCase = false, int depth = -1, bool force = false) => Apewer.Json.From(@this, lowerCase, depth, force);
/// 填充类型实例,失败时返回 NULL 值。
/// 将要解析的对象。
/// 忽略属性名称大小写。
/// 忽略的属性名称字符。
/// 强制填充,忽略 的 Serializable 特性。
public static T Object(this Json @this, bool ignoreCase = true, string ignoreChars = null, bool force = false) where T : class, new() => Apewer.Json.Object(@this, ignoreCase, ignoreChars, force);
/// 填充类型实例,失败时返回 NULL 值。
/// 将要解析的对象。
/// 忽略属性名称大小写。
/// 忽略的属性名称字符。
/// 强制填充,忽略 的 Serializable 特性。
public static T[] Array(this Json @this, bool ignoreCase = true, string ignoreChars = null, bool force = false) where T : class, new() => Apewer.Json.Array(@this, ignoreCase, ignoreChars, force);
/// 设置属性名称为小写。
public static Json Lower(this Json @this) => Apewer.Json.Lower(@this);
/// 设置属性名称为驼峰形式。
public static Json Camel(this Json @this) => Apewer.Json.Camel(@this);
#endregion
#region Logger
/// 记录异常。
public static void Exception(this Logger logger, object sender, Exception exception) => logger?.InnerException(sender, exception);
/// 记录错误。多个 Content 参数将以“ | ”分隔。
public static void Error(this Logger logger, object sender, params object[] content) => logger?.InnerError(sender, content);
/// 记录警告。多个 Content 参数将以“ | ”分隔。
public static void Warning(this Logger logger, object sender, params object[] content) => logger?.InnerWarning(sender, content);
/// 记录警告。多个 Content 参数将以“ | ”分隔。
public static void Info(this Logger logger, object sender, params object[] content) => logger?.InnerInfo(sender, content);
/// 记录文本。多个 Content 参数将以“ | ”分隔。
public static void Text(this Logger logger, object sender, params object[] content) => logger?.InnerText(sender, content);
/// 记录调试。多个 Content 参数将以“ | ”分隔。
[Conditional("DEBUG")]
public static void Debug(this Logger logger, object sender, params object[] content) => logger?.InnerDebug(sender, content);
#endregion
#region Stream
/// 重置流的位置到开始位置。
public static bool ResetPosition(this Stream @this) => BytesUtility.ResetPosition(@this);
/// 读取源流中的数据,并将数据写入目标流,获取写入的字节数。
public static long Read(this Stream @this, Stream destination, Action progress = null) => BytesUtility.Read(@this, destination, progress);
/// 读取源流中的数据,并将数据写入目标流,获取写入的字节数。
public static long Read(this Stream @this, Stream destination, Func progress = null) => BytesUtility.Read(@this, destination, progress);
/// 读取源流中的数据,获取写入的字节。
public static byte[] Read(this Stream @this, bool dispose) => BytesUtility.Read(@this, 1024, dispose);
/// 读取源流中的数据。
public static byte[] Read(this Stream @this, int buffer = 1024, bool dispose = false) => BytesUtility.Read(@this, buffer, dispose);
/// 向流写入数据。
/// 源流。
/// 要写入的数据。
public static long Write(this Stream @this, params byte[] bytes) => BytesUtility.Write(@this, bytes);
/// 读取源流中的数据,并将数据写入当前流,获取写入的字节数。
/// 目标流。
/// 源流。
public static long Write(this Stream @this, Stream source) => BytesUtility.Read(source, @this);
/// 获取 MD5 值。
public static byte[] MD5(this Stream @this) => BytesUtility.MD5(@this);
/// 获取 SHA1 值。
public static byte[] SHA1(this Stream @this) => BytesUtility.SHA1(@this);
#if !NET20
/// 获取 SHA256 值。
public static byte[] SHA256(this Stream @this) => BytesUtility.SHA256(@this);
#endif
#endregion
#region Rrflection
/// 判断指定类型具有特性。
public static bool Contains(this ICustomAttributeProvider @this, bool inherit = false) where T : Attribute => RuntimeUtility.Contains(@this, inherit);
#endregion
#region Collection
/// 添加多个元素。
public static void Add(this List @this, params T[] items)
{
if (@this != null && items != null) @this.AddRange(items);
}
/// 添加元素。
public static bool Add(this IList> @this, TKey key, TValue value) => CollectionHelper.Add(@this, key, value);
/// 判断集合为空。
public static bool IsEmpty(this IEnumerable @this) => CollectionHelper.IsEmpty(@this);
///
public static bool NotEmpty(this IEnumerable @this) => CollectionHelper.NotEmpty(@this);
///
public static bool Contains(this IEnumerable @this, T cell) => CollectionHelper.Contains(@this, cell);
/// 获取集合中元素的数量。
public static int Count(this IEnumerable @this) => CollectionHelper.Count(@this);
/// 对元素去重,且去除 NULL 值。
public static T[] Distinct(this IEnumerable @this) => CollectionHelper.Distinct(@this);
/// 获取可枚举集合的部分元素。
public static T[] Slice(this IEnumerable @this, int start = 0, int count = -1, Func stuffer = null) => CollectionHelper.Slice(@this, start, count, stuffer);
/// 安全转换为 List< > 对象。可指定排除 NULL 值元素。
///
public static List List(this IEnumerable @this, bool excludeNull = false) => CollectionHelper.ToList(@this, excludeNull);
/// 安全转换为 < >[] 对象。可指定排除 NULL 值元素。
///
public static T[] Array(IEnumerable @this, bool excludeNull = false) => CollectionHelper.ToArray(@this, excludeNull);
/// 对列表中的元素排序。
public static List Sort(this List @this, Func comparison) => CollectionHelper.Sort(@this, comparison);
/// 对字典中的键排序。
public static Dictionary SortKey(this Dictionary @this, Func comparison) => CollectionHelper.SortKey(@this, comparison);
/// 对字典中的键排序。
public static Dictionary SortKey(this Dictionary @this) where TKey : IComparable => CollectionHelper.SortKey(@this, (a, b) => a.CompareTo(b));
/// 对字典中的值排序。
public static Dictionary SortValue(this Dictionary @this, Func comparison) => CollectionHelper.SortValue(@this, comparison);
/// 对字典中的值排序。
public static Dictionary SortValue(this Dictionary @this) where TValue : IComparable => CollectionHelper.SortValue(@this, (a, b) => a.CompareTo(b));
/// 获取集合中的第一个元素。可指定失败时的默认返回值。
public static T First(this IEnumerable collection, T failed = default(T)) => CollectionHelper.First(collection, failed);
/// 获取集合中的最后一个元素。可指定失败时的默认返回值。
public static T Last(this IEnumerable collection, T failed = default(T)) => CollectionHelper.Last(collection, failed);
/// 生成 StringPairs 对象实例为副本。
public static StringPairs StringPairs(this NameValueCollection @this) => Apewer.StringPairs.From(@this);
/// 解析源对象。
public static Dictionary Origin(this TextSet instance) => instance == null ? null : instance.Origin;
/// 解析源对象。
public static Dictionary Origin(this ObjectSet instance) => instance == null ? null : instance.Origin;
/// 解析源对象。
public static Dictionary Origin(this ObjectSet instance) => instance == null ? null : instance.Origin;
#endregion
#region Source
/// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。
public static Class DateTime(this IQuery @this, int row = 0, int column = 0) => @this == null ? null : Query.DateTime(@this.Value(row, column));
/// 获取默认表中指定单元格的内容。从第 0 行开始。
public static Class DateTime(this IQuery @this, int row, string column) => @this == null ? null : Query.DateTime(@this.Value(row, column));
/// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。
public static Int32 Int32(this IQuery @this, int row = 0, int column = 0) => @this == null ? 0 : Int32(@this.Text(row, column));
/// 获取默认表中指定单元格的内容。从第 0 行开始。
public static Int32 Int32(this IQuery @this, int row, string column) => @this == null ? 0 : Int32(@this.Text(row, column));
/// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。
public static Int64 Int64(this IQuery @this, int row = 0, int column = 0) => @this == null ? 0L : Int64(@this.Text(row, column));
/// 获取默认表中指定单元格的内容。从第 0 行开始。
public static Int64 Int64(this IQuery @this, int row, string column) => @this == null ? 0L : Int64(@this.Text(row, column));
/// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。
public static Decimal Decimal(this IQuery @this, int row = 0, int column = 0) => @this == null ? 0M : Decimal(@this.Text(row, column));
/// 获取默认表中指定单元格的内容。从第 0 行开始。
public static Decimal Decimal(this IQuery @this, int row, string column) => @this == null ? 0M : Decimal(@this.Text(row, column));
/// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。 >
public static Double Double(this IQuery @this, int row = 0, int column = 0) => @this == null ? 0D : Double(@this.Text(row, column));
/// 获取默认表中指定单元格的内容。从第 0 行开始。 >
public static Double Double(this IQuery @this, int row, string column) => @this == null ? 0D : Double(@this.Text(row, column));
/// 获取默认表中指定单元格的内容。从第 0 行第 0 列开始。
public static string Text(this IQuery @this, int row = 0, int column = 0) => @this == null ? null : Query.Text(@this.Value(row, column));
/// 获取默认表中指定单元格的内容。从第 0 行开始。
public static string Text(this IQuery @this, int row, string column) => @this == null ? null : Query.Text(@this.Value(row, column));
#endregion
#region Web
/// 获取参数,指定可能的参数名,默认将修剪参数值。
/// 当从 URL 中获取参数时将解码。
public static string Parameter(this ApiRequest @this, params string[] names) => ApiUtility.Parameter(@this, true, names);
/// 设置 status 为 error,并设置 message 的内容。
public static void Error(this ApiResponse @this, string message = "未知错误。") => ApiUtility.Error(@this, message);
/// 输出字节数组。
public static void Bytes(this ApiResponse @this, byte[] bytes, string type = "application/octet-stream") => ApiUtility.Model(@this, type, new ApiBytesModel() { Bytes = bytes });
/// 输出 UTF-8 文本。
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 });
/// 输出 Json 文本。
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 });
/// 输出文件。
public static void File(this ApiResponse @this, string path, string name = null) => ApiUtility.Model(@this, null, new ApiFileModel() { Path = path, Attachment = name });
/// 重定向。
public static void Redirect(this ApiResponse @this, string location) => ApiUtility.Model(@this, null, new ApiRedirectModel() { Location = location });
/// 设置响应,当发生错误时设置响应。返回错误信息。
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);
/// 设置响应,当发生错误时设置响应。返回错误信息。
public static string Set(this ApiResponse @this, IRecord record, bool lower = true) => ApiUtility.Respond(@this, record, lower);
/// 设置响应,当发生错误时设置响应。返回错误信息。
public static string Set(this ApiResponse @this, Json data, bool lower = true) => ApiUtility.Respond(@this, data, lower);
#endregion
}