public static T[,] Random <T>(this T[,] array) { if (array == null) { throw CommonExceptions.ArgumentNull("array"); } Contract.Ensures(Contract.Result <T[, ]>() != null); int w = array.GetLength(1); int idx = array.Length; for (int i = array.GetLength(0) - 1; i >= 0; i--) { for (int j = w - 1; j >= 0; j--) { int r = RandomExt.Next(idx--); int x = r % w; int y = r / w; if (y != i || x != j) { T temp = array[i, j]; array[i, j] = array[y, x]; array[y, x] = temp; } } } return(array); }
/// <summary> /// 基于参数类型,从给定的方法集中选择一个方法。 /// 允许通过指定 <see cref="BindingFlags.OptionalParamBinding"/> 来匹配可选参数。 /// </summary> /// <param name="bindingAttr"><see cref="System.Reflection.BindingFlags"/> 值的按位组合。</param> /// <param name="match">用于匹配的候选方法集。</param> /// <param name="types">用于定位匹配方法的参数类型。</param> /// <param name="modifiers">使绑定能够处理在其中修改了类型的参数签名的参数修饰符数组。</param> /// <returns>如果找到,则为匹配的方法;否则为 <c>null</c>。</returns> public override MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers) { if (match == null) { throw CommonExceptions.ArgumentNull("match"); } if (match.Length == 0) { throw CommonExceptions.ArrayEmpty("match"); } // 构造方法信息数组。 MatchInfo[] infos = new MatchInfo[match.Length]; MatchInfo info = null; int idx = 0; for (int i = 0; i < match.Length; i++) { if (match[i] != null) { info = new MatchInfo(match[i]); int len = info.Parameters.Length > types.Length ? info.Parameters.Length : types.Length; info.ParamOrder = info.ParamOrderRev = MethodExt.GetParamOrder(len); infos[idx++] = info; } } info = SelectMethod(bindingAttr, infos, idx, types); if (info == null) { return(null); } return(info.Method); }
public static void To(this uint source, uint destination, Action <uint> action) { if (action == null) { throw CommonExceptions.ArgumentNull("action"); } Contract.EndContractBlock(); if (source < destination) { while (source < destination) { action(source); source++; } action(source); } else { while (source > destination) { action(source); source--; } action(source); } }
/// <summary> /// 通过使用指定的 <see cref="IEqualityComparer{T}"/> 对值进行比较返回序列中的重复元素。 /// </summary> /// <typeparam name="TSource"><paramref name="source"/> 中的元素的类型。</typeparam> /// <param name="source">要从中得到重复元素的序列。</param> /// <param name="comparer">用于比较值的 <see cref="IEqualityComparer{T}"/>。</param> /// <returns>一个序列,包含源序列中的重复元素。</returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> 为 <c>null</c>。</exception> public static IEnumerable <TSource> Iterative <TSource>(this IEnumerable <TSource> source, IEqualityComparer <TSource> comparer) { if (source == null) { throw CommonExceptions.ArgumentNull("source"); } Contract.EndContractBlock(); Dictionary <TSource, bool> dict = new Dictionary <TSource, bool>(comparer); foreach (TSource item in source) { bool flag; if (dict.TryGetValue(item, out flag)) { if (flag) { yield return(item); dict[item] = false; } } else { dict.Add(item, true); } } }
/// <summary> /// 从当前数组的指定索引开始截取指定长度的一部分。 /// 如果 <paramref name="startIndex"/> 小于 <c>0</c>, /// 那么表示从字符串结束位置向前计算的位置。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="array">要截取的数组。</param> /// <param name="startIndex">要截取的起始索引。</param> /// <param name="length">要截取的数组元素个数。</param> /// <returns>截取得到的数组。</returns> /// <exception cref="System.ArgumentNullException"> /// <paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="startIndex"/> 小于负的此数组的长度。</exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="length"/> 小于 <c>0</c>。</exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="startIndex"/> 加 <paramref name="length"/> /// 之和指示的位置不在此数组中。</exception> public static T[] Subarray <T>(this T[] array, int startIndex, int length) { if (array == null) { throw CommonExceptions.ArgumentNull("array"); } if (startIndex < -array.Length) { throw CommonExceptions.ArgumentOutOfRange("startIndex", startIndex); } if (length < 0 || startIndex + length > array.Length) { throw CommonExceptions.InvalidOffsetLength(); } Contract.Ensures(Contract.Result <T[]>() != null); if (length == 0) { return(Empty <T>()); } if (startIndex < 0) { startIndex += array.Length; } return(SubarrayInternal(array, startIndex, startIndex + length)); }
/// <summary> /// 返回序列中满足指定条件的最后一个元素的索引。 /// </summary> /// <typeparam name="TSource"><paramref name="source"/> 中的元素的类型。</typeparam> /// <param name="source">要从中返回元素索引的 <see cref="IEnumerable{T}"/>。</param> /// <param name="predicate">用于测试每个元素是否满足条件的函数。</param> /// <returns>序列中通过指定谓词函数中的测试的最后一个元素的索引。如果没有这样的元素,则返回 <c>-1</c>。</returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="predicate"/> 为 <c>null</c>。</exception> public static int LastIndex <TSource>(this IEnumerable <TSource> source, Func <TSource, bool> predicate) { if (source == null) { throw CommonExceptions.ArgumentNull("source"); } if (predicate == null) { throw CommonExceptions.ArgumentNull("predicate"); } Contract.EndContractBlock(); int lastIdx = -1; int idx = 0; foreach (TSource item in source) { if (predicate(item)) { lastIdx = idx; } idx++; } return(lastIdx); }
/// <summary> /// 向当前数组的指定索引插入指定的项,并返回新数组。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="array">当前数组。</param> /// <param name="index">新项要插入的索引。</param> /// <param name="items">要插入的项。</param> /// <returns>数组插入项后的结果。</returns> /// <exception cref="ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 小于 <c>0</c> 或大于数组的长度。</exception> public static T[] Insert <T>(this T[] array, int index, params T[] items) { if (array == null) { throw CommonExceptions.ArgumentNull("array"); } if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index > array.Length) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.Ensures(Contract.Result <T[]>() != null); if (items == null || items.Length == 0) { return(array); } int len = array.Length + items.Length; T[] result = new T[len]; if (index > 0) { Array.Copy(array, 0, result, 0, index); } items.CopyTo(result, index); Array.Copy(array, index, result, index + items.Length, array.Length - index); return(result); }
/// <summary> /// 将多个数组合并为一个数组。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="arrays">要合并的数组。</param> /// <returns>数组的合并结果。</returns> /// <exception cref="ArgumentNullException"><paramref name="arrays"/> 为 <c>null</c>。</exception> public static T[] Combine <T>(params T[][] arrays) { if (arrays == null) { throw CommonExceptions.ArgumentNull("arrays"); } Contract.Ensures(Contract.Result <T[]>() != null); int len = arrays.Sum(arr => arr == null ? 0 : arr.Length); if (len == 0) { return(Empty <T>()); } T[] result = new T[len]; int idx = 0; for (int i = 0; i < arrays.Length; i++) { if (arrays[i] != null) { arrays[i].CopyTo(result, idx); idx += arrays[i].Length; } } return(result); }
/// <summary> /// 从当前数组的指定索引开始截取到指定索引结束的一部分。 /// 如果 <paramref name="startIndex"/> 或 <paramref name="endIndex"/> /// 小于 <c>0</c>,那么表示从数组末尾向前计算的位置。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="array">要截取的数组。</param> /// <param name="startIndex">要截取的起始索引。</param> /// <param name="endIndex">要截取的结束索引,但不包括该位置的元素。</param> /// <returns>截取得到的数组。如果 <paramref name="startIndex"/> 等于数组的长度或大于等于 /// <paramref name="endIndex"/> ,则为空数组。</returns> /// <exception cref="ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> 或 <paramref name="endIndex"/> /// 指示的位置不在此数组中。</exception> public static T[] Slice <T>(this T[] array, int startIndex, int endIndex) { if (array == null) { throw CommonExceptions.ArgumentNull("array"); } if (startIndex < -array.Length || startIndex > array.Length) { throw CommonExceptions.ArgumentOutOfRange("startIndex", startIndex); } if (endIndex < -array.Length || endIndex > array.Length) { throw CommonExceptions.ArgumentOutOfRange("endIndex", endIndex); } Contract.Ensures(Contract.Result <T[]>() != null); if (startIndex == endIndex) { return(Empty <T>()); } if (startIndex < 0) { startIndex += array.Length; } if (endIndex < 0) { endIndex += array.Length; } if (startIndex >= endIndex) { return(Empty <T>()); } T[] result = new T[endIndex - startIndex]; Array.Copy(array, startIndex, result, 0, result.Length); return(result); }
public static T[, ,] Random <T>(this T[, ,] array) { if (array == null) { throw CommonExceptions.ArgumentNull("array"); } Contract.Ensures(Contract.Result <T[, , ]>() != null); int h = array.GetLength(1); int w = array.GetLength(2); int idx = array.Length; for (int i = array.GetLength(0) - 1; i >= 0; i--) { for (int j = h - 1; j >= 0; j--) { for (int k = w - 1; k >= 0; k--) { int r = RandomExt.Next(idx--); int t = r / w; int x = r - t * w; // r % w int z = t / h; int y = t - z * h; // t % h if (z != i || y != j || x != k) { T temp = array[i, j, k]; array[i, j, k] = array[z, y, x]; array[z, y, x] = temp; } } } } return(array); }
/// <summary> /// 从当前数组的指定索引开始截取到指定索引结束的一部分。 /// 如果 <paramref name="startIndex"/> 或 <paramref name="endIndex"/> /// 小于 <c>0</c>,那么表示从数组末尾向前计算的位置。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="array">要截取的数组。</param> /// <param name="startIndex">要截取的起始索引。</param> /// <param name="endIndex">要截取的结束索引,但不包括该位置的元素。</param> /// <returns>截取得到的数组。如果 <paramref name="startIndex"/> 等于数组的长度或大于等于 /// <paramref name="endIndex"/> ,则为空数组。</returns> /// <exception cref="System.ArgumentNullException"> /// <paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="startIndex"/> 或 <paramref name="endIndex"/> /// 指示的位置不在此数组中。</exception> public static T[] Slice <T>(this T[] array, int startIndex, int endIndex) { if (array == null) { throw CommonExceptions.ArgumentNull("array"); } if (startIndex < -array.Length || startIndex > array.Length) { throw CommonExceptions.ArgumentOutOfRange("startIndex", startIndex); } if (endIndex < -array.Length || endIndex > array.Length) { throw CommonExceptions.ArgumentOutOfRange("endIndex", endIndex); } Contract.Ensures(Contract.Result <T[]>() != null); if (startIndex == endIndex) { return(Empty <T>()); } if (startIndex < 0) { startIndex += array.Length; } if (endIndex < 0) { endIndex += array.Length; } if (startIndex < endIndex) { return(SubarrayInternal(array, startIndex, endIndex)); } return(Empty <T>()); }
/// <summary> /// 从此实例检索子字符串。子字符串从指定的字符位置开始,从指定的字符位置结束。 /// 如果 <paramref name="startIndex"/> 或 <paramref name="endIndex"/> /// 小于 <c>0</c>,那么表示从字符串结束位置向前计算的位置。 /// </summary> /// <param name="str">要检索子字符串的字符串实例。</param> /// <param name="startIndex">此示例中子字符串的起始字符位置(从零开始)。</param> /// <param name="endIndex">此示例中子字符串的结束字符位置(从零开始),但不包括该位置的字符。</param> /// <returns>与此实例中在 <paramref name="startIndex"/> 处开头、在 <paramref name="endIndex"/> /// 处结束的子字符串等效的一个字符串,如果 <paramref name="startIndex"/> 等于此实例的长度或大于等于 /// <paramref name="endIndex"/> ,则为 <see cref="String.Empty"/>。</returns> /// <exception cref="ArgumentNullException"><paramref name="str"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> 或 <paramref name="endIndex"/> /// 指示的位置不在此实例中。</exception> /// <overloads> /// <summary> /// 从此实例检索子字符串。 /// </summary> /// </overloads> public static string Slice(this string str, int startIndex, int endIndex) { if (str == null) { throw CommonExceptions.ArgumentNull("str"); } if (startIndex < -str.Length) { throw CommonExceptions.ArgumentOutOfRange("startIndex", startIndex); } if (endIndex < -str.Length) { throw CommonExceptions.ArgumentOutOfRange("endIndex", endIndex); } Contract.Ensures(Contract.Result <string>() != null); if (startIndex < 0) { startIndex += str.Length; } if (endIndex < 0) { endIndex += str.Length; } if (startIndex < endIndex) { return(str.Substring(startIndex, endIndex - startIndex)); } return(string.Empty); }
/// <summary> /// 随机打乱转序列中元素的顺序。 /// </summary> /// <typeparam name="TSource"><paramref name="source"/> 中的元素的类型。 /// </typeparam> /// <param name="source">要随机打乱顺序的值序列。</param> /// <returns>一个序列,其元素以随机顺序对应于输入序列的元素。</returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> 为 <c>null</c>。</exception> public static IEnumerable <TSource> RandomOrder <TSource>(this IEnumerable <TSource> source) { if (source == null) { throw CommonExceptions.ArgumentNull("source"); } Contract.EndContractBlock(); return(source.ToArray().Random()); }
/// <summary> /// 将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。 /// 一个参数指定该操作是否区分大小写。 /// </summary> /// <typeparam name="TEnum">要获取枚举对象的枚举类型。</typeparam> /// <param name="value">包含要转换的值或名称的字符串。</param> /// <param name="ignoreCase"><c>true</c> 为忽略大小写;<c>false</c> 为考虑大小写。</param> /// <returns><typeparamref name="TEnum"/> 类型的对象,其值由 <paramref name="value"/> 表示。</returns> /// <exception cref="ArgumentNullException"><paramref name="value"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentException"><typeparamref name="TEnum"/> 不是 <see cref="System.Enum"/>。</exception> /// <exception cref="ArgumentException"><paramref name="value"/> 是空字符串 ("") 或只包含空白。</exception> /// <exception cref="ArgumentException"><paramref name="value"/> 是一个名称,但不是为该枚举定义的命名常量之一。 /// </exception> /// <exception cref="OverflowException"><paramref name="value"/> 超出 <typeparamref name="TEnum"/> /// 基础类型的范围。</exception> public static TEnum Parse <TEnum>(string value, bool ignoreCase) { if (value == null) { throw CommonExceptions.ArgumentNull("value"); } Contract.EndContractBlock(); return((TEnum)Enum.Parse(typeof(TEnum), value, ignoreCase)); }
/// <summary> /// 添加将对象从 <typeparamref name="TInput"/> 类型转换为 <typeparamref name="TOutput"/> 类型的转换器。 /// </summary> /// <param name="converter">将对象从 <typeparamref name="TInput"/> 类型转换为 /// <typeparamref name="TOutput"/> 类型的转换器。</param> /// <exception cref="ArgumentNullException"><paramref name="converter"/> 为 <c>null</c>。</exception> /// <remarks><paramref name="converter"/> 不会覆盖预定义的隐式、显式类型转换和用户自定义的类型转换。 /// 对于相同输入/输出类型的 <paramref name="converter"/>,后设置的会覆盖先设置的,以及任何 /// <see cref="IConverterProvider"/> 提供的类型转换方法。</remarks> public static void AddConverter <TInput, TOutput>(Converter <TInput, TOutput> converter) { if (converter == null) { throw CommonExceptions.ArgumentNull("converter"); } Contract.EndContractBlock(); ConversionFactory.AddConverterProvider(new ConverterProvider(converter, typeof(TInput), typeof(TOutput))); }
/// <summary> /// 用随机数填充指定字节数组的元素。 /// </summary> /// <param name="buffer">包含随机数的字节数组。</param> /// <exception cref="ArgumentNullException"><paramref name="buffer"/> 为 <c>null</c>。</exception> public static void NextBytes(byte[] buffer) { if (buffer == null) { throw CommonExceptions.ArgumentNull("buffer"); } Contract.EndContractBlock(); Random.NextBytes(buffer); }
/// <summary> /// 将数组使用指定的值填充。 /// </summary> /// <param name="array">要填充的数组。</param> /// <param name="value">要填充数组的值。</param> /// <returns>填充完毕的数组。</returns> /// <exception cref="System.ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> /// <overloads> /// <summary> /// 将数组使用指定的值填充。 /// </summary> /// </overloads> public static Array Fill(this Array array, object value) { if (array == null) { throw CommonExceptions.ArgumentNull("array"); } Contract.Ensures(Contract.Result <Array>() != null); return(FillInternal(array, value, 0, array.Length)); }
/// <summary> /// 将数组使用指定的值填充。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="array">要填充的数组。</param> /// <param name="value">要填充数组的值。</param> /// <returns>填充完毕的数组。</returns> /// <exception cref="System.ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> public static T[] Fill <T>(this T[] array, T value) { if (array == null) { throw CommonExceptions.ArgumentNull("array"); } Contract.Ensures(Contract.Result <T[]>() != null); return(FillInternal(array, value, 0, array.Length)); }
/// <summary> /// 使用指定的实例属性,初始化 <see cref="MemberAccessor{TTarget, TValue}"/> 类的新实例, /// 表示指定的实例属性。 /// </summary> /// <param name="property">要访问的实例属性。</param> /// <param name="nonPublic">指示是否应访问非公共实例属性。 /// 如果要访问非公共实例属性,则为 <c>true</c>;否则为 <c>false</c>。</param> /// <exception cref="ArgumentNullException"><paramref name="property"/> 为 <c>null</c>。</exception> public MemberAccessor(PropertyInfo property, bool nonPublic) { if (property == null) { throw CommonExceptions.ArgumentNull("property"); } Contract.EndContractBlock(); this.name = property.Name; Init(property, nonPublic); }
/// <summary> /// 添加指定的类型转换器提供者。 /// </summary> /// <param name="provider">要添加的类型转换器提供者。</param> /// <exception cref="ArgumentNullException"><paramref name="provider"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="provider"/> 的源类型为 <c>null</c>。</exception> /// <remarks><paramref name="provider"/> 提供的类型转换方法不会覆盖预定义的隐式、显式类型转换和用户自定义的类型转换, /// 以及任何通过 <see cref="AddConverter{TInput, TOutput}"/> 方法设置的类型转换方法。 /// 对于相同源类型的 <see cref="IConverterProvider"/>,<see cref="IConverterProvider.GetConverterTo"/> /// 方法提供的类型转换方法优先级更高,且后设置的优先级更高。</remarks> public static void AddConverterProvider(IConverterProvider provider) { CommonExceptions.CheckArgumentNull(provider, nameof(provider)); if (provider.OriginType == null) { throw CommonExceptions.ArgumentNull(nameof(provider)); } Contract.EndContractBlock(); ConversionFactory.AddConverterProvider(provider); }
/// <summary> /// 使用指定的字段,初始化 <see cref="MemberAccessor{TTarget, TValue}"/> 类的新实例, /// 表示指定的字段。 /// </summary> /// <param name="field">要访问的字段。</param> /// <exception cref="ArgumentNullException"><paramref name="field"/> 为 <c>null</c>。</exception> public MemberAccessor(FieldInfo field) { if (field == null) { throw CommonExceptions.ArgumentNull("field"); } Contract.EndContractBlock(); this.name = field.Name; Init(field); }
public static IEnumerable <T> Times <T>(this uint source, Func <T> value) { if (value == null) { throw CommonExceptions.ArgumentNull("value"); } Contract.Ensures(Contract.Result <IEnumerable <T> >() != null); for (uint i = 0; i < source; i++) { yield return(value()); } }
public static void Times(this uint source, Action <uint> action) { if (action == null) { throw CommonExceptions.ArgumentNull("action"); } Contract.EndContractBlock(); for (uint i = 0; i < source; i++) { action(i); } }
/// <summary> /// 检索指定枚举类型中常数描述和值的集合。如果不存在描述,则使用其名称。 /// </summary> /// <param name="enumType">枚举类型。</param> /// <returns>一个 <see cref="TextValuePairCollection"/> 实例,其中包含 /// <paramref name="enumType"/> 中实例的描述和值。该集合的元素按枚举的二进制值排序。</returns> /// <exception cref="ArgumentNullException"><paramref name="enumType"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentException"><paramref name="enumType"/> 不是 <see cref="Enum"/>。</exception> /// <overloads> /// <summary> /// 检索指定枚举类型中常数描述和值的集合。 /// </summary> /// </overloads> public static TextValuePairCollection GetDescValues(Type enumType) { if (enumType == null) { throw CommonExceptions.ArgumentNull("type"); } if (!enumType.IsEnum) { throw CommonExceptions.MustBeEnum("type", enumType); } Contract.Ensures(Contract.Result <TextValuePairCollection>() != null); return(GetTextValues(enumType, true)); }
/// <summary> /// 返回具有指定输入和输出类型的 <see cref="Converter{TInput, TOutput}"/> 类型。 /// </summary> /// <param name="inputType">要转换的对象的类型。</param> /// <param name="outputType">要将输入对象转换到的类型。</param> /// <returns>具有指定输入和输出类型的 <see cref="Converter{TInput, TOutput}"/> 类型。</returns> /// <exception cref="ArgumentNullException"><paramref name="inputType"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="outputType"/> 为 <c>null</c>。</exception> public static Type GetConverterType(Type inputType, Type outputType) { if (inputType == null) { throw CommonExceptions.ArgumentNull("inputType"); } if (outputType == null) { throw CommonExceptions.ArgumentNull("outputType"); } Contract.EndContractBlock(); return(typeof(Converter <,>).MakeGenericType(inputType, outputType)); }
/// <summary> /// 指示指定字符串中位于指定位置处的字符是否属于十六进制数字类别。 /// </summary> /// <param name="str">一个字符串。</param> /// <param name="index">要计算的字符在 <paramref name="str"/> 中的位置。</param> /// <returns>如果 <paramref name="str"/> 中位于 <paramref name="index"/> 处的字符是十进制数字, /// 则为 <c>true</c>;否则,为 <c>false</c>。</returns> /// <exception cref="ArgumentNullException"><paramref name="str"/> 为 <c>null</c>。</exception> /// <exception cref="IndexOutOfRangeException"><paramref name="index"/> 大于等于字符串的长度或小于零。</exception> public static bool IsHex(string str, int index) { if (str == null) { throw CommonExceptions.ArgumentNull("str"); } if (index < 0 || index >= str.Length) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.EndContractBlock(); return(IsHex(str[index])); }
/// <summary> /// 将数组从指定的索引位置开始使用指定的值填充。 /// </summary> /// <param name="array">要填充的数组。</param> /// <param name="value">要填充数组的值。</param> /// <param name="startIndex">要开始填充的起始索引。</param> /// <returns>填充完毕的数组。</returns> /// <exception cref="System.ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="startIndex"/> /// 小于零或大于等于数组的长度。</exception> public static Array Fill(this Array array, object value, int startIndex) { if (array == null) { throw CommonExceptions.ArgumentNull("array"); } if (startIndex < 0 || startIndex >= array.Length) { throw CommonExceptions.ArgumentOutOfRange("startIndex", startIndex); } Contract.Ensures(Contract.Result <Array>() != null); return(FillInternal(array, value, startIndex, array.Length - startIndex)); }
/// <summary> /// 添加指定的类型转换器提供者。 /// </summary> /// <param name="provider">要添加的类型转换器提供者。</param> /// <exception cref="ArgumentNullException"><paramref name="provider"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="provider"/> 的源类型为 <c>null</c>。</exception> /// <remarks><paramref name="provider"/> 提供的类型转换方法不会覆盖预定义的隐式、显式类型转换和用户自定义的类型转换, /// 以及任何通过 <see cref="AddConverter{TInput, TOutput}"/> 方法设置的类型转换方法。 /// 对于相同源类型的 <see cref="IConverterProvider"/>,<see cref="IConverterProvider.GetConverterTo"/> /// 方法提供的类型转换方法优先级更高,且后设置的优先级更高。</remarks> public static void AddConverterProvider(IConverterProvider provider) { if (provider == null) { throw CommonExceptions.ArgumentNull("provider"); } if (provider.OriginType == null) { throw CommonExceptions.ArgumentNull("provider.OriginType"); } Contract.EndContractBlock(); ConversionFactory.AddConverterProvider(provider); }
/// <summary> /// 使用包含实例属性或字段的类型和名称,初始化 <see cref="MemberAccessor{TTarget, TValue}"/> 类的新实例, /// 表示指定的实例属性或字段。 /// </summary> /// <param name="targetType">包含实例属性或字段的类型。</param> /// <param name="name">实例属性或字段的名称。</param> /// <param name="nonPublic">指示是否应访问非公共实例属性或字段。 /// 如果要访问非公共实例属性或字段,则为 <c>true</c>;否则为 <c>false</c>。</param> /// <exception cref="ArgumentNullException"><paramref name="targetType"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentException"><paramref name="name"/> 为 <c>null</c> 或空字符串。</exception> public MemberAccessor(Type targetType, string name, bool nonPublic) { if (targetType == null) { throw CommonExceptions.ArgumentNull("targetType"); } if (string.IsNullOrEmpty(name)) { throw CommonExceptions.StringEmpty("name"); } Contract.EndContractBlock(); this.name = name; this.Init(targetType, nonPublic); }
/// <summary> /// 使用包含属性的对象和字段信息,初始化 <see cref="MemberAccessor{T}"/> 类的新实例, /// 表示指定的实例字段。 /// </summary> /// <param name="target">包含实例字段的对象。</param> /// <param name="field">要访问的实例字段。</param> /// <exception cref="ArgumentNullException"><paramref name="target"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="field"/> 为 <c>null</c>。</exception> public MemberAccessor(object target, FieldInfo field) { if (target == null) { throw CommonExceptions.ArgumentNull("target"); } if (field == null) { throw CommonExceptions.ArgumentNull("field"); } Contract.EndContractBlock(); this.name = field.Name; Init(field, target); }