Skip to main content

Disposify by ruccho

NuGet / site data​

Nuget GitHub last commit GitHub Repo stars

Details​

Info​

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

Source: https://github.com/ruccho/Disposify

Author​

note

ruccho Alt text

Original Readme​

note

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 return dynamic but 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​

note

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)​

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>

Generated Files​

Those are taken from $(BaseIntermediateOutputPath)\GX

namespace Disposify
{
internal static partial class DisposifyInternal
{
}
}

Useful​

Download Example (.NET C#)​

Share Disposify​

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Disposify

Category "Disposer" has the following generators:​

1 BenutomoAutomaticDisposeImplSourceGenerator Nuget GitHub Repo stars 2023-08-15

2 Coplt.Dropping Nuget GitHub Repo stars 2024-08-13

3 Dirge Nuget GitHub Repo stars 2026-07-01

4 DisposableHelpers Nuget GitHub Repo stars 2023-10-09

5 Disposer Nuget GitHub Repo stars 2023-10-03

6 Disposify Nuget GitHub Repo stars 2026-11-11

7 GenerateDispose Nuget GitHub Repo stars 2026-05-13

8 IDisposableGenerator Nuget GitHub Repo stars 2023-10-11

9 ReflectionIT.DisposeGenerator Nuget GitHub Repo stars 2026-08-19

See category​

Disposer