AttributeCollection.Matches 方法

定义

确定指定的特性或特性数组是否与集合中的特性或特性数组相同。

重载

Matches(Attribute)

确定指定的特性是否与集合中的特性相同。

Matches(Attribute[])

确定指定数组中的特性是否与集合中的特性相同。

Matches(Attribute)

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

确定指定的特性是否与集合中的特性相同。

public:
 bool Matches(Attribute ^ attribute);
public bool Matches (Attribute attribute);
public bool Matches (Attribute? attribute);
member this.Matches : Attribute -> bool
Public Function Matches (attribute As Attribute) As Boolean

参数

attribute
Attribute

要与集合中的特性进行比较的 Attribute 的实例。

返回

如果特性包含在集合中,且其值与集合中该特性的值相同,则为 true;否则为 false

示例

下面的代码示例验证 是否 BrowsableAttribute 是 集合的成员,并且它是否已设置为 true。 它假定 button1 已在窗体上创建了 和 textBox1

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.";
      }
   }
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.";
 }
Private Sub MatchesAttribute
    ' Creates a new collection and assigns it the attributes for button
    Dim attributes As AttributeCollection
    attributes = TypeDescriptor.GetAttributes(button1)

    ' Checks to see if the browsable attribute is true.
    If attributes.Matches(BrowsableAttribute.Yes) Then
        textBox1.Text = "button1 is browsable."
    Else
        textBox1.Text = "button1 is not browsable."
    End If
End Sub

注解

特性可以提供匹配支持。

和 方法之间的区别Matches在于,Matches对属性调用 Match 方法,并Contains调用 Equals 方法。Contains

对于大多数属性,这些方法执行相同操作。 但是,对于可能具有多个标志的属性, Match 通常会实现 ,以便在满足任何标志时返回 true 。 例如,假设数据绑定属性具有布尔标志“SupportsSql”、“SupportsOleDb”和“SupportsXml”。 此属性可能存在于支持所有三种数据绑定方法的属性上。 通常,程序员需要知道特定方法是否可用,而不是全部三种方法。 因此,程序员可以将 与仅包含程序员所需标志的属性实例一起使用 Match

另请参阅

适用于

Matches(Attribute[])

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

确定指定数组中的特性是否与集合中的特性相同。

public:
 bool Matches(cli::array <Attribute ^> ^ attributes);
public bool Matches (Attribute[] attributes);
public bool Matches (Attribute[]? attributes);
member this.Matches : Attribute[] -> bool
Public Function Matches (attributes As Attribute()) As Boolean

参数

attributes
Attribute[]

要与此集合中的特性进行比较的 MemberAttributes 的数组。

返回

如果数组中的所有特性都包含在集合中,且其值与集合中特性的值相同,则为 true;否则为 false

示例

下面的代码示例比较按钮和文本框中的属性,以查看它们是否匹配。 它假定 button1 已在窗体上创建了 和 textBox1

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.
      array<Attribute^>^ myAttrArray = gcnew array<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.";
      }
   }
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.";
}
Private Sub MatchesAttributes()
    ' Creates a new collection and assigns it the attributes for button1.
    Dim myCollection As AttributeCollection
    myCollection = TypeDescriptor.GetAttributes(button1)
       
    ' Checks to see whether the attributes in myCollection match the attributes.
    ' for textBox1.
    Dim myAttrArray(100) As Attribute
    TypeDescriptor.GetAttributes(textBox1).CopyTo(myAttrArray, 0)
    If myCollection.Matches(myAttrArray) Then
        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."
    End If
End Sub

注解

特性可以提供匹配支持。

另请参阅

适用于