I have a object graph that is setup like this
Clients
string Name
List[Address] Addresses
I would like to cast this to
MyClients: Clients
string Name
List[MyAddress] Addresses
MyAddress: Address
String City
I know that I can cast this by walking the whole object graph and in the given example that would not be to bad. But if you have a large object graph it gets hairy very fast and secondly it just seems plain wrong to have to walk the whole object graph.
I am looking for a solution that
In a comment under your original question, you say:
And yet in the question you say a good solution:
A cast isn’t able to add capabilities to an object. The object already has to have those capabilities.
So it sounds like you don’t want to change the types, you just want to add extra facilities to those types within certain areas of your solution.
So you need extension methods.
You’d keep the types
ClientandAddress, but add additional facilities via extension methods:This allows you to write:
You may need to store extra data along with each
Addressobject. Probably the most convenient off-the-shelf solution to this is to makeAddressderive from DependencyObject, which allows you to stash extra data, using an instance of DependencyProperty as the key:This way, you can effectively add new properties to existing object, and can use them like this: