views:

84

answers:

3

I am writing code that will populate the Margin, Padding and BorderThickness properties of classes in the System.Windows.Documents namespace. Each of these properties accepts a value in the form of a System.Windows.Thickness, which is a struct.

However, I wish to associate some additional data with each of these property assignments, which may subsequently be retrieved by my code. If Thickness were a class, I would inherit from it and define properties in the subclass to store my additional data items. But since it is a struct, inheritance is not possible.

Is there any practical way to achieve this, while maintaining type-compatibility with the properties I am populating?

Thanks for your ideas,

Tim

+2  A: 

There are no good alternatives.

Depending on what you are trying to do, you could perhaps define your own class with the properties you need, and define the implicit conversion operator to do a implicit conversion to the correct struct type. Then you would be able to pass in your class to all methods expecting a Thickness parameter.

This would go against the recommendation of using the implicit conversion operator, though, since it states that the implicit conversion should not lose any information. You will not be able to get the Thickness back from the property you are reading, and see the extra information you attached.

This is how you could implement it:

public class ThicknessEx
{
    public string ExtraData { get; set; }
    public Thickness Thickness { get; set; }

    public static implicit operator Thickness(ThicknessEx rhs)
    {
        return rhs.Thickness;
    }
}

However, you are probably better off by storing the extra data elsewhere. How to do it would depend on your needs and application.

driis
Thanks for a very innovative solution. As you say, it almost works, but since the extra data is not retrievable from the properties to which it was assigned, it means that it must be duplicated elsewhere, which is somewhat self-defeating. I tried adapting your idea to work with the underlying dependency properties, via the SetValue() method, but SetValue() performs type checking which seems to be an obstacle to passing in anything except the expected type.
Tim Coulter
A: 

Could you not store a Dictionary where the key is the hash code of the struct?

AndrewVos
That would depend on whether two equal Thickness instances is required to have the same value in the extra data, or not.
driis
Hmm, good point.
AndrewVos
+1  A: 

You might be able to use Attached Dependency Properties of type AugmentedThickness and then, when they change, update the underlying properties they are intended to update. This requires all access to be performed using your Attached Properties, as simply setting the Thickness property will not use your AugmentedThickness. If necessary, you could also (though it might be a bit evil) listen for explicit changes to Thickness properties (that you didn't initiate) and force it back to the value specified by your AugmentedThickness.

Dan Bryant