public static void ForEachWithRef <T>(this T[] array, ActionWithRef <T> action) { for (int i = 0; i < array.Length; ++i) { action(ref array[i]); } }
internal static void ForEachRef <T>(this T[,] array, ActionWithRef <T> action) { for (int i = 0; i < array.GetLength(0); i++) { for (int j = 0; j < array.GetLength(1); j++) { action(ref array[i, j]); } } }
/// <summary> /// 对两个集合进行同步迭代, 对每一对元素进行操作. /// </summary> /// <typeparam name="T1">第1个集合里面元素的类型</typeparam> /// <typeparam name="T2">第2个集合里面元素的类型</typeparam> /// <param name="collection1">第1个集合</param> /// <param name="collection2">第2个集合</param> /// <param name="actionWithRef">对每对元素的引用所执行的操作</param> /// <exception cref="TwoIEnumerableCountNotMatchException">第1个集合的元素数大于第2个集合的元素数时会抛出 CountNotMatchException 异常.</exception> public static void ForEachPair <T1, T2>(T1[] collection1, T2[] collection2, ActionWithRef <T1, T2> actionWithRef) { for (int i = 0; i < collection1.Length; i++) { if (i > collection2.Length - 1) { throw new DimensionNotMatchException(); } actionWithRef(ref collection1[i], ref collection2[i]); } }
/// <summary> /// Invokes the <see cref="CompositeStateParticipant{T}.TryInvokeActionWithRef"/> method with given delegate, assuming that field type is same as event type. /// </summary> /// <typeparam name="TEvent">The type of the property.</typeparam> /// <param name="evt">The <see cref="CompositeEvent{T}"/>.</param> /// <param name="action">The delegate to invoke.</param> /// <exception cref="InvalidOperationException">If <see cref="CompositeStateParticipant{T}.TryInvokeFunctionWithRef"/> returns <c>false</c>, that is, when the field type does not match <typeparamref name="TEvent"/>.</exception> /// <remarks> /// TODO link to documentation about how field type is deduced (it is not always the same as type of property or event). /// </remarks> /// <seealso cref="CompositeStateParticipant{T}.TryInvokeActionWithRef"/> public static void InvokeActionWithRefSameType <TEvent>(this CompositeEvent <TEvent> evt, ActionWithRef <TEvent> action) where TEvent : class { evt.InvokeActionWithRef(action); }
/// <summary> /// Invokes the <see cref="CompositeStateParticipant{T}.TryInvokeActionWithRef"/> method with given delegate, assuming that field type is same as property type. /// </summary> /// <typeparam name="TProperty">The type of the property.</typeparam> /// <param name="property">The <see cref="CompositeProperty{T}"/>.</param> /// <param name="action">The delegate to invoke.</param> /// <exception cref="InvalidOperationException">If <see cref="CompositeStateParticipant{T}.TryInvokeFunctionWithRef"/> returns <c>false</c>, that is, when the field type does not match <typeparamref name="TProperty"/>.</exception> /// <remarks> /// TODO link to documentation about how field type is deduced (it is not always the same as type of property or event). /// </remarks> /// <seealso cref="CompositeStateParticipant{T}.TryInvokeActionWithRef"/> public static void InvokeActionWithRefSameType <TProperty>(this CompositeProperty <TProperty> property, ActionWithRef <TProperty> action) { property.InvokeActionWithRef(action); }
/// <summary> /// Helper method to invoke <see cref="CompositeStateParticipant{T}.TryInvokeActionWithRef"/> and throw an exception if it returns <c>false</c>. /// </summary> /// <typeparam name="TReflectionInfo">The kind of composite state participant, <see cref="PropertyInfo"/> or <see cref="EventInfo"/>.</typeparam> /// <typeparam name="TField">The presumed type of the field containing composite property or event.</typeparam> /// <param name="stateParticipant">The <see cref="CompositeStateParticipant{T}"/>.</param> /// <param name="action">The delegate to invoke.</param> /// <exception cref="InvalidOperationException">If <see cref="CompositeStateParticipant{T}.TryInvokeActionWithRef"/> returns <c>false</c>, that is, when the field type does not match <typeparamref name="TField"/>.</exception> /// <remarks> /// TODO link to documentation about how field type is deduced (it is not always the same as type of property or event). /// </remarks> /// <seealso cref="CompositeStateParticipant{T}.TryInvokeActionWithRef"/> public static void InvokeActionWithRef <TReflectionInfo, TField>(this CompositeStateParticipant <TReflectionInfo> stateParticipant, ActionWithRef <TField> action) where TReflectionInfo : MemberInfo { if (!stateParticipant.TryInvokeActionWithRef(action)) { ThrowUnmatchedStateParticipantFieldType <TReflectionInfo, TField>(stateParticipant); } }
public Boolean TryInvokeActionWithRef <TField>(ActionWithRef <TField> action) { Object result; return(this._refInvoker(action, out result)); }
/// <summary> /// 对两个集合进行同步迭代, 对每一对元素的引用以及当前索引进行操作. /// </summary> /// <typeparam name="T1">第1个集合里面元素的类型</typeparam> /// <typeparam name="T2">第2个集合里面元素的类型</typeparam> /// <param name="collection1">第1个集合</param> /// <param name="collection2">第2个集合</param> /// <param name="action">需要对每对元素执行的操作, 接受两个集合的元素引用以及当前索引作为参数. </param> /// <param name="forceDimensionMatching">为true, 会在操作后检查两个集合元素数是否不相等. 为false, 不检查.</param> /// <param name="ifCheckDimensionFirst">如果为true, 会在迭代之前检查集合元素数量是否匹配. 可与<paramref name="forceDimensionMatching"/>配合使用</param> /// <exception cref="CollectionCountNotMatchException{T1, T2}">当1)集合2的元素数量小于集合1的元素数量, 或2)<paramref name="forceDimensionMatching"/>为true, 且两个集合元素数不相等时, 会抛出 CountNotMatchException 异常.</exception> public static void ForEachPair <T1, T2>(IList <T1> collection1, IList <T2> collection2, ActionWithRef <T1, T2, int> action, bool forceDimensionMatching = false, bool ifCheckDimensionFirst = false) { collection1.ShouldBeNotNullArgument(nameof(collection1)); collection2.ShouldBeNotNullArgument(nameof(collection2)); if (ifCheckDimensionFirst) { checkDimension(collection1, collection2, forceDimensionMatching); } for (int i = 0; i < collection1.Count; i++) { if (i >= collection2.Count) { throw new CollectionCountNotMatchException <T1, T2>(collection1, collection2); } var item1 = collection1[i]; var item2 = collection2[i]; action(ref item1, ref item2, ref i); collection1[i] = item1; collection2[i] = item2; } checkDimension(collection1, collection2, forceDimensionMatching); }