Runtime properties

Runtime properties

Old forum URL: forums.lhotka.net/forums/t/2407.aspx


Clayton posted on Thursday, February 22, 2007

We have a business case where we need to set properties that we only know at runtime. It would be too difficult to program every conceivable runtime scenario for updating the properties.

What we've done to get around this is to add the code below to BusinessBase<T>. Does anyone have a more efficient way to do this? Is it useful enough that a variant could be added to CSLA?

private static PropertyDescriptorCollection _properties = System.ComponentModel.TypeDescriptor.GetProperties(typeof(T));

public object this[string propertyName]
{
    get
    {
        return _properties[propertyName].GetValue(this);
    }
    set
    {
        _properties[propertyName].SetValue(this, value);
    }
}

RockfordLhotka replied on Thursday, February 22, 2007

The solution to your problem is probably the Decorator design pattern - which is similar to what you've done.

But Decorator formalizes the separation slightly, by having the extra properties in a collection that is referenced by your object. In other words, don't make your object itself into a collection by adding an indexer, but rather have your object expose a read-only property to provide a reference to your decorator collection.

Copyright (c) Marimer LLC