Compartir vía


Stand-alone Unity Interception

You can use Unity interception as a stand-alone feature with no dependency injection container by using the Intercept class. As with a container, interception as a stand-alone feature enables you to perform instance or type interception. The Intercept class contains the NewInstance,NewInstanceWithAdditionalInterfaces,ThroughProxy, and ThroughProxyWithAdditionalInterfaces methods, enabling you to perform either proxy or instance interception. And both methods include the AdditionalInterfaces parameter, enabling you to implement additional interfaces on the target object. This corresponds to the AdditionalInterface feature when using interception with a container.

Note

The first parameter on Intercept.ThroughProxy, Intercept.ThroughProxyWithAdditionalInterfaces and Intercept. NewInstance is the corresponding interceptor when setting up interception through the stand-alone API.

The AdditionalInterfaces parameter on the object enables you to receive more messages and to augment the set of methods the object can respond to.

This section contains the following sections describing stand-alone interception:

  • Stand-Alone Interception with a Proxy describes creating a proxy to the intercepted instance.
  • Stand-Alone Interception with a Derived Type describes interception by using a derived type.

Stand-Alone Interception with a Proxy

Instance interceptors work by creating a proxy to the intercepted instance. The following examples create a Proxy interface interceptor. The first example implements no additional interfaces.

IInterface proxy = Intercept.ThroughProxy<IInterface>(
        new BaseClass(10),
        new InterfaceInterceptor(),
        new[] { interceptionBehavior }
);
'Usage
Dim proxy As IInterface = Intercept.ThroughProxy(Of IInterface)( _
        New BaseClass(10), _
        New InterfaceInterceptor(), _
        New IInterceptionBehavior() {interceptionBehavior} _
)

The following example uses the last parameter to specify additional interfaces to implement. You can specify as many interfaces as you like.

IInterface proxy = (IInterface)Intercept.ThroughProxyWithAdditionalInterfaces(
        typeof(IInterface),
        new BaseClass(10),
        new InterfaceInterceptor(),
        new[] { interceptionBehavior },
        new[] { typeof(ISomeInterface), typeof(IsomeOtherInterface) });
'Usage
Dim proxy As IInterface = DirectCast(Intercept.ThroughProxyWithAdditionalInterfaces( _
        GetType(IInterface), _
        New BaseClass(10), _
        New InterfaceInterceptor(), _
        New IInterceptionBehavior() { interceptionBehavior }, _
        New Type() { GetType(ISomeInterface), GetType(IsomeOtherInterface) })

Stand-alone Interception with a Derived Type

You can use Intercept.NewInstance to set up type interception using a derived type. You can use the additionalInterfaces parameter to implement additional interfaces on the target object. You can provide any number of interfaces for this parameter. Unity provides one derived type interceptor: the virtual method interceptor. The last parameter contains arguments for the creation of the new instance and is an arbitrary number in this example.

BaseClass instance =
    Intercept.NewInstance<BaseClass>(
        new VirtualMethodInterceptor(),
        new[] { interceptionBehavior },
        10);
'Usage
Dim instance As BaseClass = _
    Intercept.NewInstance(Of BaseClass) _
        (New VirtualMethodInterceptor(), _
        New IInterceptionBehavior() {interceptionBehavior}, _
        10)

This is the same as the previous example, except additional interfaces are specified.

BaseClass instance =
    Intercept.NewInstanceWithAdditionalInterfaces<BaseClass>(
        new VirtualMethodInterceptor(),
        new[] { interceptionBehavior },
        new[] { typeof(ISomeInterface), typeof(IsomeOtherInterface)},
        10);
'Usage
Dim instance As BaseClass = _
    Intercept.NewInstanceWithAdditionalInterfaces(Of BaseClass)( _
        New VirtualMethodInterceptor(), _
        New IInterceptionBehavior() {interceptionBehavior}, _
        New Type(){GetType(ISomeInterface), GetType(IsomeOtherInterface)}, _
        10)