I have an object handed into our library and passed through various processes. I need to attach some additional information to these objects as they pass through various stages and out the other end - a kind of dynamic decorator pattern, I guess, except adding additional properties rather than changing existing behaviour.
I was hoping to use LinFu or Castle to create a dynamic proxy and implement an additional interface on the object to store this. Components that know about the extended interface could cast and access it - whilst those that are not are oblivious, as the underlying type has not changed.
However, I hadn't appreciated that all these mechanisms assume you have control over the point at which the type is initially created - which I don't.
Does anyone have suggestions on how I could better approach this?
Many thanks
This doesn't address your scenario exactly but how about using a DynamicObject implementation that acts as a decorator around your object? It would allow you to access the original object as well as additional properties. Sort of like ExpandoObject but starting out with your own instance data.
Something like this:
It doesn't address your strong typing/interface requirement unfortunately though and performance isn't going to be the greatest given the Reflection usage here (from https://github.com/RickStrahl/Westwind.Utilities/blob/master/Westwind.Utilities/Utilities/ReflectionUtils.cs)