API reference
API reference
Select your platform
No SDKs available
No versions available

ClassToClassDecorator Class

Base class for Context-based decoration, specifically where both the instance being decorated (InstanceT ) and the data being used as a decoration (DecorationT ) are class types.
Automatically removes decorations when the decorated instance is cleaned up.
Others
InstanceT
The type of the instance being decorated
DecorationT
The type of the decoration being added to instances
As described in the remarks for DecoratorBase<InstanceT, DecorationT>, the simplest way to associate an instance with a decoration is to create a map or Dictionary from InstanceT to DecorationT , then store the association there. One often-unintended effect of this is that, because C# is a managed language, both the instance and the decoration will be "pinned into existence": because the (long-lived) decorator dictionary contains references to the instance, the garbage collector will never be able to clean it up, even if nothing else refers to it, which means the fact of being decorated can change the lifespan of the instance. ClassToClassDecorator avoids this problem by capturing only a weak reference to the instance (the decoration is still strongly referenced and thus "pinned into existence"). Consequently, decorating instances using a ClassToClassDecorator is a non-invasive operation which will not alter the lifespan or other behavior of the decorated instance.

Protected Functions

ClassToClassDecorator ( )

Member Functions

Associates an instance with a decoration.
If this instance was already decorated, the old decoration is removed. If there were any outstanding asynchronous requests for the decoration of this instance (from prior calls to GetDecorationAsync(InstanceT), those requests will be fulfilled as part of this operation.
Parameters
instance
The instance to be decorated
decoration
The decoration to be added to this instance
Removes the association between an instance and a decoration.
If this instance had no decoration, this does nothing. If a decoration was removed, note that GetDecorationAsync(InstanceT) will resume returning uncompleted tasks for this instance, which will be completed if and when the instance is decorated again.
Parameters
instance
The instance from which decoration should be removed
Synchronously retrieves the decoration associated with an instance, if one is available.
Parameters
instance
The instance for which to retrieve the decoration
decoration
Out parameter to be populated with the decoration if one exists, otherwise null
Returns
True if decoration was populated with a valid decoration, otherwise false
Asynchronously retrieves the decoration associated with an instance.
If the instance already has a decoration, the returned task is immediately completed with the currently-available decoration. If no decoration is yet available for this instance, the returned task will only be completed during the call to AddDecoration(InstanceT, DecorationT) for the instance in question.
Parameters
instance
The instance for which to retrieve the decoration
Returns
A task resulting in the decoration associated with instance
Did you find this page helpful?
Thumbs up icon
Thumbs down icon