/// <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); } }
/// <summary> /// Helper method to invoke <see cref="CompositeStateParticipant{T}.TryInvokeFunctionWithRef"/> 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="function">The delegate to invoke.</param> /// <returns>The return value of of <paramref name="function"/>.</returns> /// <exception cref="InvalidOperationException">If <see cref="CompositeStateParticipant{T}.TryInvokeFunctionWithRef"/> 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}.TryInvokeFunctionWithRef"/> public static TField InvokeFunctionWithRef <TReflectionInfo, TField>(this CompositeStateParticipant <TReflectionInfo> stateParticipant, FunctionWithRef <TField> function) where TReflectionInfo : MemberInfo { TField result; if (!stateParticipant.TryInvokeFunctionWithRef(function, out result)) { ThrowUnmatchedStateParticipantFieldType <TReflectionInfo, TField>(stateParticipant); } return(result); }
private static void ThrowUnmatchedStateParticipantFieldType <TReflectionInfo, TField>(CompositeStateParticipant <TReflectionInfo> stateParticipant) where TReflectionInfo : MemberInfo { throw new InvalidOperationException("The composite state participant " + stateParticipant + " does not use field of type " + typeof(TField) + "."); }