Dynamic Properties (for .NET)

What is project Dynamic Properties?

This project allows you easily add dynamic properties to any existing instance without creating derived type in .NET (C# or VB.NET). This approach is similar as attached dependency properties in WPF. It can be used as attribute system for complex definition of data model behavior or just to create temporary properties on existing object model.

How can I use it?

// define dynamic property for any reference type - "bool" property for "Customer" type
var dpMyProperty = new DynamicProperty<Customer, bool>("MyProperty", defaultValue: false);

// set property value to any instance
dpMyProperty.Set(customer, true);

// and read it when you need it
bool isSelected = dpMyProperty.Get(customer);

Why should I use it?

There is lot of situations when you need to add some properties to classes in C# or VB.NET. In most cases you can simply add property to class definition or create derived class type with new property or use something like decorator design pattern. But it is not always possible to do. For example:
  • Class definition is sealed or in assembly of 3rd party libraries (including .NET Framework) so you cannot change it.
  • Adding properties to class is impossible because there are lot of other depending assemblies that do not expect changes of classes design.
  • You don't want to change class because new property is used only is some specific parts of application.
In all of these cases you can use dynamic properties. It allows you to easily create property definition and then assign value of this property to any reference type instance without changing its code or creating derived class.

Download with Nuget

You can download Dynamic Properties binaries as Nuget package.
Name: DynamicProperties (http://nuget.org/List/Packages/DynamicProperties)
But if you can't use Nuget for some reason (or simply you don't like it) then you can download binaries or source code manually of course.

Is this something like attached dependency property in WPF?

Yes. In Windows Presentation Foundation there is feature called attached dependency property that you can use for defining new properties on existings data types. But there is one major restriction - this can be used only on types derived from DependencyObject which is not possible to do in most cases. But Dynamic Properties can be used with any reference data type.

What are features of Dynamic Properties?

  • Dynamic properties objects are generic so you can use properties manipulation methods as strongly-typed with full intellisense.
  • You can attach callback to OnChanged event on specific object or globally on property definition so you can react to every change of specific dynamic property.
  • You can use 3 types of default values for dynamic properties:
    1. default - null for reference types, default(T) for value types
    2. static - default value is shared
    3. delegate - you code take care about providing default value instance
  • Dynamic properties are linked to target objects with weak refence so it will be automatically disposed by GC when your code stop using it - just as you expected with normal objects.
  • You can use your own specific instance of Extender class responsible for storing values of dynamic properties in memory. So you can use Dynamic Properties in isolated part of your application or independently in several places. But in most cases you do not need to know this and use only default global storage in Extender.Global.
  • Methods of Dynamic Properties are thread safe. That means you can use it for example to notify UI about progress of processing collection in different thread.
  • Simple usage, only one assembly, no useless namespace prefix - only DynamicProperties namespace.
  • Unit tests included!

Quick how to samples

How to subscribe for OnChange event?

Subscribe global "OnChange" event to recieve all property changes:
var dpMyProperty = new DynamicProperty<Customer, int>("MyProperty", OnChange);

public static void OnChange(Customer target, DynamicPropertyChangedParams<Customer, int> args) {
    // your code here
}
Or subscribe local "OnChange" event so you recieve property changes for specific object instance:
Extender.Global.RegisterOnChanged(someInstance, dpMyProperty, OnChange);

public static void OnChange(Customer target, DynamicPropertyChangedParams<Customer, int> args) {
    // your code here
}

How to check if value is defined?

int result;
if(dpMyProperty.TryGet(someInstance, out result))
{
    // value is defined!
}

Available samples

Sample: ColoredStrings

Simple console application shows how to attach dynamic property to string.
coloredStrings.png

Sample: AsyncProgressNotify

This WPF project contains simple "Task" class with only "Name" property. There is also asynchnrounous processor of this task that use dynamic property to let UI know about its progress.
asyncProccess.png

Last edited Sep 24, 2011 at 5:17 PM by jechtom, version 34