Disposify by ruccho
NuGet / site data
Details
Info
Name: Disposify
Disposify is a source generator to subscribe / unsubscribe C# events with using-IDisposable pattern.
Author: ruccho
NuGet: https://www.nuget.org/packages/Disposify/
You can find more details at https://github.com/ruccho/Disposify
Author
ruccho

Original Readme
Disposify
[!WARNING] Currently very experimental.
Disposify is a source generator to subscribe / unsubscribe C# events with using-dispose pattern.
using System;
using Disposify;
var c = new C();
using (c.Disposify().SomeEvent(v => ++v)) // subscribe with lambda
{
c.Invoke(100);
} // unsubscribe on Dispose()
public class C
{
public event Func<int, int>? SomeEvent;
public int Invoke(int a) => SomeEvent.Invoke(a);
}
It is also useful to convert callbacks into await pattern.
using System;
using System.Threading.Tasks;
using Disposify;
var c = new C();
var tcs = new TaskCompletionSource<int>();
using (c.Disposify().SomethingCompleted(result => tcs.TrySetResult(result)))
{
var result = await tcs.Task;
}
public class C
{
public event Action<int>? SomethingCompleted;
}
Installation
######### NuGet
Install from https://www.nuget.org/packages/Disposify/ .
######### Unity
Unity 2022.3.12f1 and later is supported.
Add a git dependency to package manager:
https://github.com/ruccho/Disposify.git?path=/Disposify.Unity/Packages/com.ruccho.disposify
Features
- Disposifying events declared in non-generic classes
######### Not supported yet
- Disposifying events declared in generic types and value types
Performance
- Returned
IDisposables are automatically pooled after the disposal. Disposify()seems to returndynamicbut is replaced by performant overload (no dynamic, no boxed) by the source generator.
Usages
######### Disposifying static events
For instance types, use ((T)null).Disposify().
using System;
using Disposify;
using (((C)null).Disposify().SomeEvent(v => ++v))
{
C.Invoke(100);
}
public class C
{
public static event Func<int, int>? SomeEvent;
public static int Invoke(int a) => SomeEvent.Invoke(a);
}
For static types, use [GenerateDisposifier(typeof(T))].
using System;
using Disposify;
using (C_Disposifier.SomeEvent(v => ++v))
{
C.Invoke(100);
}
public static class C
{
public static event Func<int, int>? SomeEvent;
public static int Invoke(int a) => SomeEvent.Invoke(a);
}
[GenerateDisposifier(typeof(C))]
static partial class C_Disposifier
{
// members will be generated
}
About
Subscribing and unsubscribing C# events with the using-dispose pattern
Purpose
Disposify generates typed IDisposable subscriptions for events. A subscription is automatically removed when its disposable scope ends.
How to Use
var publisher = new Publisher();
using (publisher.Disposify().Changed(value =>
{
Console.WriteLine($"Received: {value}");
}))
{
publisher.Raise("while subscribed");
}
publisher.Raise("after disposal");
The source generator replaces the dynamic-looking Disposify call with a performant typed overload.
How to use
Example (source csproj, source files)
- CSharp Project
- Program.cs
- Publisher.cs
This is the CSharp Project that references Disposify
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Disposify" Version="0.1.3" PrivateAssets="all" />
</ItemGroup>
</Project>
This is the use of Disposify in Program.cs
using Disposify;
var publisher = new Publisher();
Console.WriteLine("Before subscription:");
publisher.Raise("no listener");
using (publisher.Disposify().Changed(value =>
Console.WriteLine($"Received: {value}")))
{
Console.WriteLine("Inside subscription:");
publisher.Raise("while subscribed");
}
Console.WriteLine("After disposal:");
publisher.Raise("no listener");
This is the use of Disposify in Publisher.cs
namespace Disposify;
public sealed class Publisher
{
public event Action<string>? Changed;
public void Raise(string value)
{
Changed?.Invoke(value);
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- DefaultDisposifierExtension.g.cs
- Disposifiers.g.cs
namespace Disposify
{
internal static partial class DisposifyInternal
{
}
}
#nullable enable
namespace Disposify
{
partial class DisposifyInternal
{
public static global::Disposify.Generated.Disposify.PublisherDisposifier Disposify(this global::Disposify.Publisher? target) => new global::Disposify.Generated.Disposify.PublisherDisposifier(target);
}
}
namespace Disposify.Generated
{
namespace Disposify
{
internal readonly struct PublisherDisposifier
{
private readonly global::Disposify.Publisher? Target;
public PublisherDisposifier(global::Disposify.Publisher? target) => Target = target;
public global::Disposify.Disposable Changed(global::System.Action<string> @delegate)
{
Target!.Changed += @delegate;
return global::Disposify.Disposable.Create(Target, @delegate, static (target, @delegate) => target.Changed -= @delegate);
}
}
\} // namespace Disposify
} // namespace Disposify.Generated
Useful
Download Example (.NET C#)
Share Disposify
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Disposify
Category "Disposer" has the following generators:
1 BenutomoAutomaticDisposeImplSourceGenerator
2023-08-15
2 Coplt.Dropping
2024-08-13
3 Dirge
2026-07-01
4 DisposableHelpers
2023-10-09
5 Disposer
2023-10-03
6 Disposify
2026-11-11
7 GenerateDispose
2026-05-13
8 IDisposableGenerator
2023-10-11
9 ReflectionIT.DisposeGenerator
2026-08-19