AttributeCollection.Contains 方法

定义

确定该特性集合是否具有指定的特性或特性数组。

重载

Contains(Attribute)

确定该特性集合是否具有指定的特性。

Contains(Attribute[])

确定该特性集合是否包含特性数组中所有指定的特性。

Contains(Attribute)

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

确定该特性集合是否具有指定的特性。

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

参数

attribute
Attribute

要在集合中查找的 Attribute

返回

如果集合包含该特性或是该类型特性的默认特性,则为 true;否则为 false

示例

下面的代码示例检查集合是否将 BrowsableAttribute 设置为 true。 它假定 button1 已在窗体上创建了 和 textBox1

protected:
   void ContainsAttribute()
   {
      // Creates a new collection and assigns it the attributes for button1.
      AttributeCollection^ attributes;
      attributes = TypeDescriptor::GetAttributes( button1 );
      
      // Sets an Attribute to the specific attribute.
      BrowsableAttribute^ myAttribute = BrowsableAttribute::Yes;

      if ( attributes->Contains( myAttribute ) )
      {
         textBox1->Text = "button1 has a browsable attribute.";
      }
      else
      {
         textBox1->Text = "button1 does not have a browsable attribute.";
      }
   }
private void ContainsAttribute() {
    // Creates a new collection and assigns it the attributes for button1.
    AttributeCollection attributes;
    attributes = TypeDescriptor.GetAttributes(button1);

    // Sets an Attribute to the specific attribute.
    BrowsableAttribute myAttribute = BrowsableAttribute.Yes;

    if (attributes.Contains(myAttribute))
       textBox1.Text = "button1 has a browsable attribute.";
    else
       textBox1.Text = "button1 does not have a browsable attribute.";
 }
Private Sub ContainsAttribute
    ' Creates a new collection and assigns it the attributes for button.
    Dim attributes As AttributeCollection
    attributes = TypeDescriptor.GetAttributes(button1)

    ' Sets an Attribute to the specific attribute.
    Dim myAttribute As BrowsableAttribute = BrowsableAttribute.Yes

    If Attributes.Contains(myAttribute) Then
        textBox1.Text = "button1 has a browsable attribute."
    Else
        textBox1.Text = "button1 does not have a browsable attribute."
    End If
End Sub

注解

如果集合中存在指定的属性类型,并且指定属性的值与集合中特性实例的值相同,则此集合具有指定的属性。

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

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

另请参阅

适用于

Contains(Attribute[])

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

确定该特性集合是否包含特性数组中所有指定的特性。

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

参数

attributes
Attribute[]

要在集合中查找的类型 Attribute 的数组。

返回

如果该集合包含所有特性,则为 true;否则为 false

示例

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

private:
   void ContainsAttributes()
   {
      // 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 are the attributes for textBox1.
      array<Attribute^>^ myAttrArray = gcnew array<Attribute^>(100);
      TypeDescriptor::GetAttributes( textBox1 )->CopyTo( myAttrArray, 0 );
      if ( myCollection->Contains( myAttrArray ) )
      {
         textBox1->Text = "Both the button and text box have the same attributes.";
      }
      else
      {
         textBox1->Text = "The button and the text box do not have the same attributes.";
      }
   }
private void ContainsAttributes() {
   // 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 are the attributes for textBox1.
   Attribute[] myAttrArray = new Attribute[100];
   TypeDescriptor.GetAttributes(textBox1).CopyTo(myAttrArray, 0);
   if (myCollection.Contains(myAttrArray))
      textBox1.Text = "Both the button and text box have the same attributes.";
   else
      textBox1.Text = "The button and the text box do not have the same attributes.";
}
Private Sub ContainsAttributes()
    ' 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 are the attributes for textBox1.
    Dim myAttrArray(100) As Attribute
    TypeDescriptor.GetAttributes(textBox1).CopyTo(myAttrArray, 0)
    If myCollection.Contains(myAttrArray) Then
        textBox1.Text = "Both the button and text box have the same attributes."
    Else
        textBox1.Text = "The button and the text box do not have the same attributes."
    End If
End Sub

注解

如果集合中存在所有指定的属性类型,并且指定数组中的每个属性都与集合中的属性相同,则此集合具有指定的属性数组。

另请参阅

适用于