AttachedProperties 2.2.0

dotnet add package AttachedProperties --version 2.2.0                
NuGet\Install-Package AttachedProperties -Version 2.2.0                
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="AttachedProperties" Version="2.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AttachedProperties --version 2.2.0                
#r "nuget: AttachedProperties, 2.2.0"                
#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.
// Install AttachedProperties as a Cake Addin
#addin nuget:?package=AttachedProperties&version=2.2.0

// Install AttachedProperties as a Cake Tool
#tool nuget:?package=AttachedProperties&version=2.2.0                

AttachedProperties Build Coverage

A small library for dynamically adding attached properties to any existing instance without creating a derived type. It works similar in nature to WPF's attached dependency properties however it can be used on any type and not just types deriving from DependencyObject.

NuGet Package

Install-Package AttachedProperties

Example

public class Example
{

    public AttachedProperty<string, Color> ForegroundColor =
        new AttachedProperty<string, Color>(nameof(ForegroundColor));
    
    public void SomeMethod()
    {
    	var text = "Some value";
        text.SetAttachedValue(ForegroundColor, Color.White);

        WriteLineWithColor(text);
    }
    
    public void WriteLineWithColor(string text)
    {
        var foregroundColor = text.GetAttachedValue(ForegroundColor);
        Console.ForegoundColor = foregroundColor;
        Console.WriteLine(text);    	
    }

}

When to use AttachedProperties?

There are some situations when there is a need to add additional state to an existing instance. In most cases it is possible to add a property either directtly to the class or in a derived class. But this is not always possible, for example:

  • Class defintion is sealed and/or in a third party library (inclding .NET Framework) and cannot be changed.
  • The creation of the instance where additional state is needed is not possible to change and there are other dependencies which are not expecting changes to the instance.
  • Where there is no wish to change the class since the attached property is just used in a very specific part of the solution.
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AttachedProperties:

Package Downloads
FluentRegistration

FluentRegistration

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.0 5,092 2/4/2023
2.1.0 25,184 1/26/2022
2.0.1 10,757 9/24/2017
2.0.0 943 9/24/2017
1.4.0 2,659 1/28/2017
1.3.0 2,115 6/28/2016
1.2.0 2,097 6/3/2016
1.1.0 1,813 5/27/2016
1.0.1 1,483 5/23/2016
1.0.0 1,920 5/22/2016