Giter Club home page Giter Club logo

attachedproperties's Introduction

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.

attachedproperties's People

Contributors

nwendel avatar

Stargazers

 avatar  avatar Alex Stek avatar

Watchers

 avatar

Forkers

lanicon bubdm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.