Jaa


The Custom Attribute Matching Rule

The custom attribute matching rule allows developers, operators, and administrators to select target classes based on a custom attribute type that is applied to class members.

Behavior of the Custom Attribute Matching Rule

The custom attribute matching rule does the following:

  • It uses the value of the parameters passed to it to configure the matching rule for injection.
  • It compares the attributeType value to the type of any attributes that are applied to members of the target object.
  • It searches for matching attribute types in all base classes that the target class inherits from if the inherited parameter is True.
  • It returns True if the attribute type matches the value of the attributeType parameter; if the attribute type does not match the value of the attributeType, it returns False.

The matching rules for a policy can be defined in configuration or created and applied to policies at run time. For more information about configuring matching rules at design time, see Configuration Files for Interception in the section Design-Time Configuration.

Creating a Custom Attribute Matching Rule at Run Time

The following constructor overloads can be used when creating an instance of the CustomAttributeMatchingRule class.

CustomAttributeMatchingRule(Type attributeType, bool inherited)
'Usage
CustomAttributeMatchingRule(attributeType As Type, inherited As Boolean)

The following table describes the parameters shown above.

Parameter

Description

attributeType

Type. This is the type name of the custom attribute that is applied to members of the target object, such as MyCustomAttribute.

inherited

Boolean. This specifies whether the rule should also search base classes for members that carry the custom attribute.

The following code extract shows how you can add a custom attribute matching rule to a policy using the Unity interception mechanism.

myContainer.Configure<Interception>()
           .AddPolicy("MyPolicy")
           .AddMatchingRule<CustomAttributeMatchingRule>
               (new InjectionConstructor(typeof(MyAttributeType), true))
           .AddCallHandler<MyCallHandler>
                ("MyValidator", 
                new ContainerControlledLifetimeManager());
'Usage
myContainer.Configure(Of Interception)() _
           .AddPolicy("MyPolicy") _
           .AddMatchingRule(Of CustomAttributeMatchingRule) _
                (New InjectionConstructor(GetAttribue(MyAttributeType), True)) _
           .AddCallHandler(Of MyCallHandler) _
               ("MyValidator", New ContainerControlledLifetimeManager())

The code does not show how to create the container, add the Unity interception container extension, specify an interceptor, or resolve the intercepted target object. For more information about using matching rules with interception at run time, see Registering Policy Injection Components.