@@ -137,7 +137,10 @@ internal static void ContinueTask(
137137 /// is to allow the task completion source to be continued even more after this current
138138 /// continuation.
139139 /// </remarks>
140- /// <typeparam name="TState">Type of the state object to provide to the callbacks</typeparam>
140+ /// <typeparam name="TState">
141+ /// Type of the state object to provide to the callbacks, constrained to class types to
142+ /// prevent accidental modification of pass-by-value types.
143+ /// </typeparam>
141144 /// <param name="taskToContinue">Task to continue with provided callbacks</param>
142145 /// <param name="taskCompletionSource">
143146 /// Completion source used to track completion of the continuation, see remarks for details
@@ -153,6 +156,7 @@ internal static void ContinueTaskWithState<TState>(
153156 Action < TState > onSuccess ,
154157 Action < TState , Exception > ? onFailure = null ,
155158 Action < TState > ? onCancellation = null )
159+ where TState : class
156160 {
157161 ContinuationState < TState > continuationState = new (
158162 OnCancellation : onCancellation ,
@@ -237,8 +241,14 @@ internal static void ContinueTaskWithState<TState>(
237241 /// <param name="taskCompletionSource">
238242 /// Completion source used to track completion of the continuation, see remarks for details
239243 /// </param>
240- /// <typeparam name="TState1">Type of the first state object to provide to callbacks</typeparam>
241- /// <typeparam name="TState2">Type of the second state object to provide to callbacks</typeparam>
244+ /// <typeparam name="TState1">
245+ /// Type of the first state object to provide to the callbacks, constrained to class types
246+ /// to prevent accidental modification of pass-by-value types.
247+ /// </typeparam>
248+ /// <typeparam name="TState2">
249+ /// Type of the second state object to provide to the callbacks, constrained to class types
250+ /// to prevent accidental modification of pass-by-value types.
251+ /// </typeparam>
242252 /// <param name="state1">First state object to provide to callbacks</param>
243253 /// <param name="state2">Second state object to provide to callbacks</param>
244254 /// <param name="onSuccess">Callback to execute on successful completion of the task</param>
@@ -252,6 +262,8 @@ internal static void ContinueTaskWithState<TState1, TState2>(
252262 Action < TState1 , TState2 > onSuccess ,
253263 Action < TState1 , TState2 , Exception > ? onFailure = null ,
254264 Action < TState1 , TState2 > ? onCancellation = null )
265+ where TState1 : class
266+ where TState2 : class
255267 {
256268 ContinuationState < TState1 , TState2 > continuationState = new (
257269 OnCancellation : onCancellation ,
@@ -428,7 +440,10 @@ internal static void ContinueTaskWithState<TState1, TState2>(
428440 /// task will be completed with the exception.
429441 /// * The task will be completed as successful.
430442 /// </remarks>
431- /// <typeparam name="TState">Type of the state object to pass to callbacks</typeparam>
443+ /// <typeparam name="TState">
444+ /// Type of the state object to pass to callbacks, constrained to class types to prevent
445+ /// accidental modification of pass-by-value types.
446+ /// </typeparam>
432447 /// <param name="taskToContinue">
433448 /// Task to continue with provided callbacks, if <c>null</c>, <c>null</c> will be returned.
434449 /// </param>
@@ -442,6 +457,7 @@ internal static void ContinueTaskWithState<TState1, TState2>(
442457 Action < TState > onSuccess ,
443458 Action < TState , Exception > ? onFailure = null ,
444459 Action < TState > ? onCancellation = null )
460+ where TState : class
445461 {
446462 if ( taskToContinue is null )
447463 {
@@ -533,8 +549,13 @@ internal static void ContinueTaskWithState<TState1, TState2>(
533549 /// task will be completed with the exception.
534550 /// * The task will be completed as successful.
535551 /// </remarks>
536- /// <typeparam name="TState1">Type of the first state object to pass to callbacks</typeparam>
537- /// <typeparam name="TState2">Type of the second state object to pass to callbacks</typeparam>
552+ /// <typeparam name="TState1">
553+ /// Type of the first state object to pass to callbacks, constrained to class types to
554+ /// prevent accidental modification of pass-by-value types.
555+ /// </typeparam>
556+ /// <typeparam name="TState2">
557+ /// Type of the second state object to pass to callbacks, constrained to class types to
558+ /// prevent accidental modification of pass-by-value types.</typeparam>
538559 /// <param name="taskToContinue">
539560 /// Task to continue with provided callbacks, if <c>null</c>, <c>null</c> will be returned.
540561 /// </param>
@@ -550,6 +571,8 @@ internal static void ContinueTaskWithState<TState1, TState2>(
550571 Action < TState1 , TState2 > onSuccess ,
551572 Action < TState1 , TState2 , Exception > ? onFailure = null ,
552573 Action < TState1 , TState2 > ? onCancellation = null )
574+ where TState1 : class
575+ where TState2 : class
553576 {
554577 if ( taskToContinue is null )
555578 {
@@ -667,6 +690,10 @@ internal static void SetTimeoutException(
667690 /// exception returned is set as the exception that completes the task completion source.
668691 /// This overload provides a state object to the timeout callback.
669692 /// </summary>
693+ /// <typeparam name="TState">
694+ /// Type of the state object to pass to callbacks, constrained to class types to prevent
695+ /// accidental modification of pass-by-value types.
696+ /// </typeparam>
670697 /// <param name="taskCompletionSource">Task to execute with a timeout</param>
671698 /// <param name="timeoutInSeconds">Number of seconds to wait until timing out the task</param>
672699 /// <param name="state">State object to pass to the callback</param>
@@ -681,6 +708,7 @@ internal static void SetTimeoutExceptionWithState<TState>(
681708 TState state ,
682709 Func < TState , Exception > onTimeout ,
683710 CancellationToken cancellationToken )
711+ where TState : class
684712 {
685713 if ( timeoutInSeconds <= 0 )
686714 {
@@ -786,14 +814,17 @@ private record ContinuationState<TState>(
786814 Action < TState , Exception > ? OnFailure ,
787815 Action < TState > OnSuccess ,
788816 TState State ,
789- TaskCompletionSource < object ? > TaskCompletionSource ) ;
817+ TaskCompletionSource < object ? > TaskCompletionSource )
818+ where TState : class ;
790819
791820 private record ContinuationState < TState1 , TState2 > (
792821 Action < TState1 , TState2 > ? OnCancellation ,
793822 Action < TState1 , TState2 , Exception > ? OnFailure ,
794823 Action < TState1 , TState2 > OnSuccess ,
795824 TState1 State1 ,
796825 TState2 State2 ,
797- TaskCompletionSource < object ? > TaskCompletionSource ) ;
826+ TaskCompletionSource < object ? > TaskCompletionSource )
827+ where TState1 : class
828+ where TState2 : class ;
798829 }
799830}
0 commit comments