다음을 통해 공유


The Tag Attribute Matching Rule

The tag attribute matching rule allows developers, operators, and administrators to select target classes based on the name of an attribute of type Tag that is applied to a class, or to members (methods or properties) within a class. For example, the following code shows a class with two tagged members.

public class AnnotatedWithTags
{

  [Tag("MyTagName")]
  public void TaggedMethod(string parameter1)
  { 
    ... method implementation here ...
  }

  [Tag("AnotherTagName")]
  public string TaggedProperty
  {
     ... property implementation here ... 
  }
}
'Usage
Public Class AnnotatedWithTags

  <Tag("MyTagName")> _
  Public Sub TaggedMethod(parameter1 As String)
    ... method implementation here ...
  End Sub

  <Tag("AnotherTagName")> _
  Public Property TaggedProperty As String
     ... property implementation here ... 
  End Property

End Class

The following code shows a tagged class.

[Tag("MyClassTagName")]
public class AnnotatedWithTagOnClass
{
  ... class implementation here ...
}
'Usage
<Tag("MyClassTagName")> _
Public Class AnnotatedWithTagOnClass
  ... class implementation here ...
End Class

Behavior of the Tag Attribute Matching Rule

The tag 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 tagToMatch value to the full name of the Tag attribute. Wildcard characters are not supported for this rule.
  • It performs the comparison on a non-case–sensitive basis if the ignoreCase parameter is True or on a case-sensitive basis if the ignoreCase parameter is False.
  • It returns True if the Tag attribute name matches the value of the tagToMatch parameter; if the Tag attribute does not match the value of the tagToMatch parameter, 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 Interception in the section Design-Time Configuration.

Creating a Tag Attribute Matching Rule at Run Time

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

TagAttributeMatchingRule(string tagToMatch)

TagAttributeMatchingRule(string tagToMatch, bool ignoreCase)
'Usage
TagAttributeMatchingRule(tagToMatch As String)

TagAttributeMatchingRule(tagToMatch As String, ignoreCase As Boolean)

The following table describes the parameters shown above.

Parameter

Description

tagToMatch

String. This is the name of the Tag attribute that is attached to the target object, such as MyTagName. It cannot include wildcard characters.

ignoreCase

Boolean. This specifies whether the match should be carried out on a case-sensitive basis. The default is false.

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

myContainer.Configure<Interception>()
           .AddPolicy("MyPolicy")
           .AddMatchingRule<TagAttributeMatchingRule>
               (new InjectionConstructor("MyTagName", true))
           .AddCallHandler<MyCallHandler>
           ("MyValidator", 
                new ContainerControlledLifetimeManager());
'Usage
myContainer.Configure(Of Interception)() _
           .AddPolicy("MyPolicy") _
           .AddMatchingRule(Of TagAttributeMatchingRule) _
                (New InjectionConstructor("MyTagName", 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.