TitanFx.WeakDelegate.SourceGenerator
1.1.0
dotnet add package TitanFx.WeakDelegate.SourceGenerator --version 1.1.0
NuGet\Install-Package TitanFx.WeakDelegate.SourceGenerator -Version 1.1.0
<PackageReference Include="TitanFx.WeakDelegate.SourceGenerator" Version="1.1.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="TitanFx.WeakDelegate.SourceGenerator" Version="1.1.0" />
<PackageReference Include="TitanFx.WeakDelegate.SourceGenerator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add TitanFx.WeakDelegate.SourceGenerator --version 1.1.0
#r "nuget: TitanFx.WeakDelegate.SourceGenerator, 1.1.0"
#:package TitanFx.WeakDelegate.SourceGenerator@1.1.0
#addin nuget:?package=TitanFx.WeakDelegate.SourceGenerator&version=1.1.0
#tool nuget:?package=TitanFx.WeakDelegate.SourceGenerator&version=1.1.0
TitanFx.WeakDelegate
A simple package which enables the easy creation of weak delegates. Weak delegates are delegates which dont hold onto a reference to the instance they were created from, making them perfect for use in places such as event handlers.
This packages primary purpose is to be used instead of the WeakEventManager pattern as it has much more boilerplate and requires no reflection.
Usage
There are only two real usages for this package:
Create a weak delegate from an existing delegate
using TitanFx.WeakDelegate;
// This delegate holds a reference to `myObject`
Action myDelegate = myObject.SomeMethod;
// This delegate holds a weak reference to `myObject` which allows it to be garbage collected
Action myWeakDelegate = myDelegate.ToWeakDelegate();
Create a new weak delegate directly
using TitanFx.WeakDelegate;
// This delegate holds a weak reference to `myObject` which allows it to be garbage collected
Action myWeakDelegate = Action.CreateWeakDelegate(myObject.SomeMethod);
Examples
Event handlers
A perfect use case for this is as an event handler, allowing the event source to call any handlers without preventing them from being garbage collected.
class MyModel : INotifyPropertyChanged
{
private PropertyChangedEventHandler? _propertyChanged;
public event PropertyChangedEventHandler? PropertyChanged
{
add => _propertyChanged += value?.ToWeakDelegate();
remove => _propertyChanged -= value?.ToWeakDelegate();
}
public string Name
{
get;
set
{
if (field != value)
{
field = value;
_propertyChanged?.Invoke(this, new(nameof(Name)));
}
}
}
}
class MyView
{
private readonly MyModel _model;
public MyView(MyModel model)
{
// Ordinarily this would cause a circular reference, meaning this view references the model
// and the model references the view. This would prevent the view from being garbage collected
// if the model was being referenced by something else and vice versa. However, because the model
// registers its event handlers as weak handlers, in this situation the view holds a reference to
// the model, but as soon as the view is no longer referenced by anything else it can be
// successfully collected by the GC.
_model = model;
model.PropertyChanged += HandlePropertyChanged;
}
private void HandlePropertyChanged(object? sender, PropertyChangedEventArgs e)
{
StateHasChanged();
}
}
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 5.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.