XKit.AttachedProperties 0.1.5

dotnet add package XKit.AttachedProperties --version 0.1.5
                    
NuGet\Install-Package XKit.AttachedProperties -Version 0.1.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XKit.AttachedProperties" Version="0.1.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XKit.AttachedProperties" Version="0.1.5" />
                    
Directory.Packages.props
<PackageReference Include="XKit.AttachedProperties" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add XKit.AttachedProperties --version 0.1.5
                    
#r "nuget: XKit.AttachedProperties, 0.1.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package XKit.AttachedProperties@0.1.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=XKit.AttachedProperties&version=0.1.5
                    
Install as a Cake Addin
#tool nuget:?package=XKit.AttachedProperties&version=0.1.5
                    
Install as a Cake Tool

Attached Properties for .NET

Add state to any existing object. Control the lifetime of that state with strong or weak references.

  • Attach arbitrary values (including references) to any object at runtime.
  • Use simple dynamic syntax: obj.Attached.SomeProperty = value.
  • Support composite keys for strong: obj.Attached[new Key(...)] = value and weak obj.AttachedFor(svc1, svc2).Some = value.
  • Use weak keys to scope state by service, request, or any object lifetime.

Targets: .NET Standard 2.0, .NET Standard 2.1, .NET 8, .NET 9, .NET 10

Quick Start

Test Model

	class MyClass
	{
		public string Name { get; set;  }
	}
	var myObject = new MyClass { Name = "Test" };

Strong References

Add state to attached properties:

myObject.Attached.Age = 25;

// Explanation:
// This value will live as long as myObject lives. myObject owns the state from lifetime perspective.
// The value 25 is associated by string key "Age" via dynamic binding, so all of this would work equally fine:

Console.WriteLine(myObject.Attached.Age);
Console.WriteLine(myObject.Attached["Age"]);
Console.WriteLine(((IDictionary<string, object>)myObject.Attached)["Age"]);

Add references to attached properties:

var child = new MyClass { Name = "Child" };
myObject.Attached.Child = child;

// Explanation:
// now even if you remove all references to child, it will live as long as myObject lives because it also owns the child now and keeps strong reference.

Add state by composite or reference key

Does not matter if it value type or reference, think about this as about Dictionary<object, object>. What matters is that the key is unique, comparable and the reference to key is strong.

record /*struct*/ Key(string Name, decimal version, MyClass relation);
myObject.Attached[new Key("Data", 1.0m, child)] = "Some data";

// Explanation:
// now even if you remove all references to child or Key, it will live as long as myObject lives because it also owns the key and child now and keeps strong reference.

Conclusion

If you think about it, everything is strongly referenced except the myObject itself. So the state will live as long as myObject lives, and myObject helds only Weak Reference for that purposes.

If your objects are ad-hock (e.g. Scoped Service), and you want to keep state only as long as scope lives, avoid keeping strong reference to them, you need techniques described below.

Weak References

Extend a state per service/object basis by a weak key

class MyService
{
	public void Handle(MyClass item)
	{
		item.AttachedTo(this).Age = 30; // AttachedTo(this) is the way to go!
	}
}

// Explanation:
// now if you remove all references to MyService instance, and let it be collected, the state will be partially removed as well because "this" is considered weak key.

item.AttachedTo(this, new object()).Age = 30;

// Explanation:
// you can have many weak keys per attached state. But all of them, including the item itself are weakReferences. So in this example the state will be lost in a first GC round, because noone hold new object.

Conclusion

If you think about it, attached state is like Dictionary<object, object> but they are different for every key combination. It is like if every key combination brings it's unique dictionary. And every key is equally independed weak reference. This way it does not matter who lives longer, the state will be partially removed as soon as any of the keys are collected.

To highlight that idea and philosofy even more, check out the root static method:

XKit.AttachedProperties.State.AttachedTo(params IEnumerable<object> weakKeys)

This explains that .Attached extension property is just a convention for State.AttachedTo(this)

Important notes:

Another important hint is that it does not really matter what order GetFor keys are passed. So

item.AttachedTo(worker, ledger) item.AttachedTo(ledger, worker) worker.AttachedTo(ledger, item)

This all produces the same attached state dictionary that lives as long as all keys alive. And this never extends lifetime of any of the keys.

Now if you intentionally want to extend lifetime of some key, you should assign it explicitly to owner or owners:

item.Attached(worker).Ledger = ledger; // now lifetime of ledger is extended by item and worker. Item and worker are both weakKeys and they own the "Ledger" value attached to them.

Product 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 is compatible.  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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.5 681 12/2/2025
0.1.4 519 12/1/2025
0.1.3 494 12/1/2025
0.1.2 487 12/1/2025
0.1.1 509 12/1/2025
0.1.0 431 12/1/2025