SideData 0.1.1-ci0005
dotnet add package SideData --version 0.1.1-ci0005
NuGet\Install-Package SideData -Version 0.1.1-ci0005
<PackageReference Include="SideData" Version="0.1.1-ci0005" />
<PackageVersion Include="SideData" Version="0.1.1-ci0005" />
<PackageReference Include="SideData" />
paket add SideData --version 0.1.1-ci0005
#r "nuget: SideData, 0.1.1-ci0005"
#:package SideData@0.1.1-ci0005
#addin nuget:?package=SideData&version=0.1.1-ci0005&prerelease
#tool nuget:?package=SideData&version=0.1.1-ci0005&prerelease
SideData
Attach data to any object, for the lifetime of that object.
SideData lets you associate arbitrary data with any reference-type object without modifying the object itself. The attached data lives as long as the host object; when the host is garbage collected, its side data is collected too.
Features
- Automatic cleanup: attached data is collected when the host object is GC’d
- Two APIs: ergonomic dynamic API and a strongly-typed API
- Dynamic members and indexer: bag.Property and bag["key"]
- Optional ownership and disposal of stored IDisposable/IAsyncDisposable (including opt-in dispose-on-replace)
- Thread-safe operations
- Lightweight (built on ConditionalWeakTable)
Requirements
- Target frameworks: netstandard2.0, netstandard2.1, net8.0
- Reference types only (value types are not supported as hosts)
Installation
# Package is available on NuGet
dotnet add package SideData
Quick start
Dynamic API (recommended for most scenarios)
using SideData;
var obj = new { Name = "Some object" };
// Attach and use data via dynamic members
dynamic bag = obj.SideData(); // or obj.SideData(disposeOnReplace: true)
bag.Shape = "Rectangle";
bag.NumberOfEdges = 4;
bag.EdgeLengths = new[] { 6, 4, 6, 4 };
Console.WriteLine($"A {bag.Shape} has {bag.NumberOfEdges} sides");
// Output: A Rectangle has 4 sides
Dynamic indexer support is available for string keys:
dynamic bag = obj.SideData();
bag["primary"] = new object();
var value = bag["primary"]; // same as dynamic property semantics
Strongly-typed API
Use SideDataBag when you prefer explicit, compile-time friendly access.
using SideData;
var obj = new object();
var bag = obj.SideDataBag(); // or obj.SideDataBag(disposeOnReplace: true)
// Indexer access
bag["Answer"] = 42;
int? answer = bag.GetOrDefault<int>("Answer"); // 42
// Typed helper methods
if (bag.TryGet("Answer", out int value))
{
Console.WriteLine(value);
}
// Remove/Clear (do not dispose by default)
bag.Remove("Answer");
bag.Clear();
Ownership and disposal
SideDataBag can track IDisposable and IAsyncDisposable values stored in it. By default, the bag owns these values and will dispose them when the bag is disposed or when the host object is garbage collected.
- Setting or replacing a member: the previously stored value is untracked. If the bag (or dynamic API via obj.SideData/SideDataBag) was created with disposeOnReplace: true, the replaced value will be disposed when it’s replaced. By default (disposeOnReplace: false), replacement does not dispose.
- Remove(name, disposeMember = false): removes a single member. Pass disposeMember: true to dispose the removed value if it implements IDisposable/IAsyncDisposable.
- Clear(disposeMembers = false): removes all members. Pass disposeMembers: true to dispose all removed values that implement IDisposable/IAsyncDisposable.
- Disposing the bag (Dispose or DisposeAsync): disposes all tracked IDisposable/IAsyncDisposable values owned by the bag and prevents further use.
- When the host object is collected by the GC: the associated bag is finalized and will best-effort dispose tracked IDisposable/IAsyncDisposable values.
Notes:
- Exceptions thrown during disposal are swallowed to ensure cleanup continues.
- After disposal, any operation on the bag throws ObjectDisposedException.
Thread-safety
- Each bag serializes its operations internally; individual operations are thread-safe.
- Compound operations are not atomic; if you need multi-step atomicity, use your own synchronization.
API reference (quick overview)
Extension methods:
- dynamic ObjectExtensions.SideData<T>(this T obj, bool disposeOnReplace = false) where T : class
- SideDataBag ObjectExtensions.SideDataBag<T>(this T obj, bool disposeOnReplace = false) where T : class
Dynamic usage (examples):
- bag.PropertyName = value;
- var x = bag.PropertyName;
- bag["key"] = value; var y = bag["key"];
Typed usage (SideDataBag):
- object? this[string name] { get; set; }
- IEnumerable<string> GetDynamicMemberNames()
- bool TryGet<T>(string name, out T? value)
- T? GetOrDefault<T>(string name, T? defaultValue = default)
- bool Remove(string name, bool disposeMember = false)
- void Clear(bool disposeMembers = false)
- void Dispose(); ValueTask DisposeAsync();
Low-level attachment API (advanced):
- dynamic SideDataAttachment.Get(object hostObject)
- dynamic SideDataAttachment.GetOrAdd(object hostObject, Func<dynamic>? factory = null)
- bool SideDataAttachment.TryGet(object hostObject, out dynamic value)
- void SideDataAttachment.Remove(object hostObject) // disposes the bag if present and detaches it
Tips and performance notes
- Keys are case-sensitive and compared using StringComparer.Ordinal.
- Host objects are keyed by reference identity; attaching data does not prevent host collection.
- Prefer the typed API in hot paths to avoid dynamic binder overhead.
Examples and tests
See the unit tests in tests/SideData.Tests for more examples and expected behavior.
License
MIT
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Bcl.AsyncInterfaces (>= 9.0.8)
- Microsoft.CSharp (>= 4.7.0)
-
.NETStandard 2.1
- Microsoft.Bcl.AsyncInterfaces (>= 9.0.8)
- Microsoft.CSharp (>= 4.7.0)
-
net8.0
- Microsoft.Bcl.AsyncInterfaces (>= 9.0.8)
- Microsoft.CSharp (>= 4.7.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on SideData:
Package | Downloads |
---|---|
FluentCertificates.Extensions
FluentCertificates.Extensions is part of the FluentCertificates package. This library provides a number of convenient extension-methods for exporting certificates and keys and performing other common operations. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
0.1.1-ci0005 | 149 | 8/18/2025 |
0.1.0 | 8,687 | 11/24/2022 |