AttributeCollection.Contains 方法

定義

判斷這個屬性集合是否具有指定屬性或屬性陣列。

多載

Contains(Attribute)

判斷這個屬性集合是否具有指定屬性。

Contains(Attribute[])

判斷這個屬性集合是否包含屬性陣列中所有的指定屬性。

Contains(Attribute)

來源:
AttributeCollection.cs
來源:
AttributeCollection.cs
來源:
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

範例

下列程式代碼範例會檢查集合是否設定為 BrowsableAttributetrue。 它假設 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在於呼叫 MatchMatches 屬性上的 方法,並Contains呼叫 Equals 方法。Contains

對於大部分的屬性,這些方法會執行相同的動作。 不過,對於可能有多個旗標的屬性,通常會實作 , Match 以便在滿足任何旗標時傳回 true 它。 例如,請考慮具有布爾值旗標 「SupportsSql」、“SupportsOleDb” 和 “SupportsXml” 的數據系結屬性。 這個屬性可能存在於支援所有三種數據系結方法的屬性上。 程序設計人員通常只有在特定方法可用時才需要知道,而不是這三種方法。 因此,程式設計人員可以搭配只包含程式設計人員所需旗標的屬性實例使用 Match

另請參閱

適用於

Contains(Attribute[])

來源:
AttributeCollection.cs
來源:
AttributeCollection.cs
來源:
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

範例

下列程式代碼範例會比較 中的 button1 屬性,並 textBox1 查看按鈕的屬性是否包含在文字框的屬性中。 它會假設 button1textBox1 都已在表單上建立。

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

備註

如果集合中的所有指定屬性類型都存在於集合中,而且指定的陣列中的每個屬性都與集合中的屬性相同,則此集合具有指定的屬性陣列。

另請參閱

適用於