diff --git a/Rx.NET/Source/src/System.Reactive/Disposables/Fluent/DisposableExtensions.cs b/Rx.NET/Source/src/System.Reactive/Disposables/Fluent/DisposableExtensions.cs
new file mode 100644
index 000000000..2414c1075
--- /dev/null
+++ b/Rx.NET/Source/src/System.Reactive/Disposables/Fluent/DisposableExtensions.cs
@@ -0,0 +1,38 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT License.
+// See the LICENSE file in the project root for more information.
+
+namespace System.Reactive.Disposables.Fluent;
+
+///
+/// Extension methods associated with the IDisposable interface.
+///
+public static class DisposableExtensions
+{
+ ///
+ /// Ensures the provided disposable is disposed with the specified .
+ ///
+ ///
+ /// The type of the disposable.
+ ///
+ ///
+ /// The disposable we are going to want to be disposed by the CompositeDisposable.
+ ///
+ ///
+ /// The to which will be added.
+ ///
+ ///
+ /// The disposable.
+ ///
+ public static T DisposeWith(this T item, CompositeDisposable compositeDisposable)
+ where T : IDisposable
+ {
+ if (compositeDisposable == null)
+ {
+ throw new ArgumentNullException(nameof(compositeDisposable));
+ }
+
+ compositeDisposable.Add(item);
+ return item;
+ }
+}
diff --git a/Rx.NET/Source/src/System.Reactive/System.Reactive.csproj b/Rx.NET/Source/src/System.Reactive/System.Reactive.csproj
index f03416d01..c76625412 100644
--- a/Rx.NET/Source/src/System.Reactive/System.Reactive.csproj
+++ b/Rx.NET/Source/src/System.Reactive/System.Reactive.csproj
@@ -150,7 +150,7 @@
-
+
diff --git a/Rx.NET/Source/tests/Tests.System.Reactive.ApiApprovals/Api/ApiApprovalTests.Core.verified.cs b/Rx.NET/Source/tests/Tests.System.Reactive.ApiApprovals/Api/ApiApprovalTests.Core.verified.cs
index 6e3936ec8..43e5e6796 100644
--- a/Rx.NET/Source/tests/Tests.System.Reactive.ApiApprovals/Api/ApiApprovalTests.Core.verified.cs
+++ b/Rx.NET/Source/tests/Tests.System.Reactive.ApiApprovals/Api/ApiApprovalTests.Core.verified.cs
@@ -661,6 +661,14 @@ public static System.Reactive.Disposables.ICancelable Create(params System.IDisp
public static System.Reactive.Disposables.ICancelable Create(System.IDisposable disposable1, System.IDisposable disposable2) { }
}
}
+namespace System.Reactive.Disposables.Fluent
+{
+ public static class DisposableExtensions
+ {
+ public static T DisposeWith(this T item, System.Reactive.Disposables.CompositeDisposable compositeDisposable)
+ where T : System.IDisposable { }
+ }
+}
namespace System.Reactive.Joins
{
public abstract class Pattern { }
@@ -3190,4 +3198,4 @@ public void Start(ref TStateMachine stateMachine)
where TStateMachine : System.Runtime.CompilerServices.IAsyncStateMachine { }
public static System.Runtime.CompilerServices.TaskObservableMethodBuilder Create() { }
}
-}
+}
\ No newline at end of file
diff --git a/Rx.NET/Source/tests/Tests.System.Reactive/Tests/Disposables/DisposableTests.cs b/Rx.NET/Source/tests/Tests.System.Reactive/Tests/Disposables/DisposableTests.cs
index 2c6bee05d..787265e20 100644
--- a/Rx.NET/Source/tests/Tests.System.Reactive/Tests/Disposables/DisposableTests.cs
+++ b/Rx.NET/Source/tests/Tests.System.Reactive/Tests/Disposables/DisposableTests.cs
@@ -8,6 +8,7 @@
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
+using System.Reactive.Disposables.Fluent;
using System.Threading;
using Microsoft.Reactive.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -415,6 +416,19 @@ public void CompositeDisposable_Empty_GetEnumerator()
Assert.False(composite.GetEnumerator().MoveNext());
}
+ [TestMethod]
+ public void CompositeDisposable_DisposeWith()
+ {
+ var c = new CompositeDisposable();
+ var d = new BooleanDisposable();
+ d.DisposeWith(c);
+ Assert.True(c.Contains(d));
+
+ c.Dispose();
+ Assert.True(d.IsDisposed);
+ Assert.True(c.IsDisposed);
+ }
+
[TestMethod]
public void CompositeDisposable_NonCollection_Enumerable_Init()
{