Using the Wrap Method

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

If the client application already contains an instance of an interceptable object, the Policy Injection Application Block can wrap this instance in a policy-enabled proxy instead of creating a new instance. This allows developers to use the application block to add handler pipelines to existing objects that inherit from MarshalByRefObject or implement a known interface definition.

To wrap an existing instance of an interceptable target class, the developer calls the static Wrap method of the Policy Injection Application Block factory class named PolicyInjection.

If the application block determines that the configuration specifies handlers for a method or property of the target object, it does the following:

  • It creates a suitable proxy for the object or for the interface the object implements.
  • It wires up the specified handlers into a pipeline between the relevant methods and properties of the proxy and the existing target object instance.
  • It returns a reference to the proxy.

If the application block determines that the configuration does not specify any matching handlers, the Wrap method just returns the existing instance of the target class.

To use the Wrap method, client code uses the .NET Framework generic code syntax to specify the target type name or interface name. The client code must also provide a reference to the existing object as the single parameter. The Wrap method returns an instance of a policy-enabled proxy for the existing target class instance. If the target object inherits from MarshalByRefObject, use the target object type name in the call to the Wrap method, as shown in the following code.

TargetObject theTarget = PolicyInjection.Wrap<TargetObjectType>(existingObject);
'Usage
Dim theTarget As TargetObject = PolicyInjection.Wrap(Of TargetObjectType)(existingObject)

If the target object implements a known interface, use the target object interface type name in the call to the Wrap method.

ITargetInterface theTarget = PolicyInjection.Wrap<ITargetInterface>(existingObject);
'Usage
Dim theTarget As ITargetInterface = PolicyInjection.Wrap(Of ITargetInterface)(existingObject)