AttributeCollection.Matches Method

Definition

Determines whether a specified attribute or array of attributes is the same as an attribute or array of attributes in the collection.

Overloads

Matches(Attribute)

Determines whether a specified attribute is the same as an attribute in the collection.

Matches(Attribute[])

Determines whether the attributes in the specified array are the same as the attributes in the collection.

Matches(Attribute)

Source:
AttributeCollection.cs
Source:
AttributeCollection.cs
Source:
AttributeCollection.cs

Determines whether a specified attribute is the same as an attribute in the collection.

C#
public bool Matches(Attribute? attribute);
C#
public bool Matches(Attribute attribute);

Parameters

attribute
Attribute

An instance of Attribute to compare with the attributes in this collection.

Returns

true if the attribute is contained within the collection and has the same value as the attribute in the collection; otherwise, false.

Examples

The following code example verifies that the BrowsableAttribute is a member of the collection and that it has been set to true. It assumes that button1 and textBox1 have been created on a form.

C#
private void MatchesAttribute() {
    // Creates a new collection and assigns it the attributes for button1.
    AttributeCollection attributes;
    attributes = TypeDescriptor.GetAttributes(button1);

    // Checks to see if the browsable attribute is true.
    if (attributes.Matches(BrowsableAttribute.Yes))
       textBox1.Text = "button1 is browsable.";
    else
       textBox1.Text = "button1 is not browsable.";
 }

Remarks

An attribute can provide support for matching.

The difference between the Matches and Contains methods is that Matches calls the Match method on an attribute, and Contains calls the Equals method.

For most attributes, these methods do the same thing. For attributes that may have multiple flags, however, Match is typically implemented so that it returns true if any of the flags are satisfied. For example, consider a data binding attribute with the Boolean flags "SupportsSql", "SupportsOleDb", and "SupportsXml". This attribute may be present on a property that supports all three data binding approaches. It will often be the case that a programmer needs to know only if a particular approach is available, not all three. Therefore, a programmer could use Match with an instance of the attribute containing only the flags the programmer needs.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Matches(Attribute[])

Source:
AttributeCollection.cs
Source:
AttributeCollection.cs
Source:
AttributeCollection.cs

Determines whether the attributes in the specified array are the same as the attributes in the collection.

C#
public bool Matches(Attribute[]? attributes);
C#
public bool Matches(Attribute[] attributes);

Parameters

attributes
Attribute[]

An array of MemberAttributes to compare with the attributes in this collection.

Returns

true if all the attributes in the array are contained in the collection and have the same values as the attributes in the collection; otherwise, false.

Examples

The following code example compares the attributes in a button and a text box to see whether they match. It assumes that button1 and textBox1 have been created on a form.

C#
private void MatchesAttributes() {
   // Creates a new collection and assigns it the attributes for button1.
   AttributeCollection myCollection;
   myCollection = TypeDescriptor.GetAttributes(button1);

   // Checks to see whether the attributes in myCollection match the attributes for textBox1.
   Attribute[] myAttrArray = new Attribute[100];
   TypeDescriptor.GetAttributes(textBox1).CopyTo(myAttrArray, 0);
   if (myCollection.Matches(myAttrArray))
      textBox1.Text = "The attributes in the button and text box match.";
   else
      textBox1.Text = "The attributes in the button and text box do not match.";
}

Remarks

An attribute can provide support for matching.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1