Skip to content

Enhancement Add DisposeWith #2178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Extension methods associated with the IDisposable interface.
/// </summary>
public static class DisposableExtensions
{
/// <summary>
/// Ensures the provided disposable is disposed with the specified <see cref="CompositeDisposable"/>.
/// </summary>
/// <typeparam name="T">
/// The type of the disposable.
/// </typeparam>
/// <param name="item">
/// The disposable we are going to want to be disposed by the CompositeDisposable.
/// </param>
/// <param name="compositeDisposable">
/// The <see cref="CompositeDisposable"/> to which <paramref name="item"/> will be added.
/// </param>
/// <returns>
/// The disposable.
/// </returns>
public static T DisposeWith<T>(this T item, CompositeDisposable compositeDisposable)
where T : IDisposable
{
if (compositeDisposable == null)
{
throw new ArgumentNullException(nameof(compositeDisposable));
}

compositeDisposable.Add(item);
return item;
}
}
2 changes: 1 addition & 1 deletion Rx.NET/Source/src/System.Reactive/System.Reactive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
</ItemGroup>

<ItemGroup>
<None Include="build\NuGet.Readme.md" Pack="true" PackagePath="\readme.md"/>
<None Include="build\NuGet.Readme.md" Pack="true" PackagePath="\readme.md" />
<None Include="build\_._" PackagePath="build\net6.0;build\net6.0-windows10.0.19041" Pack="true" />
<None Include="build\_._" PackagePath="buildTransitive\net6.0;buildTransitive\net6.0-windows10.0.19041" Pack="true" />
<None Include="Linq\QbservableEx.NAry.cs">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(this T item, System.Reactive.Disposables.CompositeDisposable compositeDisposable)
where T : System.IDisposable { }
}
}
namespace System.Reactive.Joins
{
public abstract class Pattern { }
Expand Down Expand Up @@ -3190,4 +3198,4 @@ public void Start<TStateMachine>(ref TStateMachine stateMachine)
where TStateMachine : System.Runtime.CompilerServices.IAsyncStateMachine { }
public static System.Runtime.CompilerServices.TaskObservableMethodBuilder<T> Create() { }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down
Loading