Extension objects pattern
- by voroninp
In this MSDN Magazine article Peter Vogel describes Extension Objects partten.
What is not clear is whether extensions can be later implemented by client code residing in a separate assembly.
And if so how in this case can extension get acces to private members of the objet being extended?
I quite often need to set different access levels for different classes.
Sometimes I really need that descendants does not have access to the mebmer but separate class does. (good old friend classes)
Now I solve this in C# by exposing callback properties in interface of the external class and setting them with private methods. This also alows to adjust access: read only or read|write depending on the desired interface.
class Parent
{
private int foo;
public void AcceptExternal(IFoo external)
{
external.GetFooCallback = () => this.foo;
}
}
interface IFoo
{
Func<int> GetFooCallback {get;set;}
}
Other way is to explicitly implement particular interface.
But I suspect more aspproaches exist.