Partager via


The Tag Attribute Matching Rule

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 Unity Application Block information can be found at the Unity Application Block site.

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, including using wildcard characters for the attribute name. 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

In more detail, the Tag Attribute Matching Rule does the following:

  • It reads the tagToMatch and ignoreCase parameters from the Unity Application Block configuration.
  • 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.

Configuration Using Constructor Injection for Tag Attribute Matching Rule

Unity uses constructor injection to generate new instances of matching rules. For information about constructor injection, see the following topics:

The following constructor overloads can be used for the TagAttributMatchingRule class.

public TagAttributeMatchingRule(
string tagToMatch
)
public TagAttributeMatchingRule(
string tagToMatch,
bool ignoreCase
)

The configuration settings available for the Tag Attribute Matching Rule consist of two parameters:

  • 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). It specifies whether the match should be carried out on a case-sensitive basis.