AttributeCollection.Contains Metoda

Definicja

Określa, czy ta kolekcja atrybutów ma określony atrybut lub tablicę atrybutów.

Przeciążenia

Contains(Attribute)

Określa, czy ta kolekcja atrybutów ma określony atrybut.

Contains(Attribute[])

Określa, czy ta kolekcja atrybutów zawiera wszystkie określone atrybuty w tablicy atrybutów.

Contains(Attribute)

Źródło:
AttributeCollection.cs
Źródło:
AttributeCollection.cs
Źródło:
AttributeCollection.cs

Określa, czy ta kolekcja atrybutów ma określony atrybut.

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

Parametry

attribute
Attribute

Element Attribute do znalezienia w kolekcji.

Zwraca

true jeśli kolekcja zawiera atrybut lub jest atrybutem domyślnym dla typu atrybutu; w przeciwnym razie , false.

Przykłady

Poniższy przykład kodu sprawdza, czy kolekcja ma ustawioną BrowsableAttribute wartość true. Przyjęto założenie, że button1 formularz i textBox1 zostały utworzone w formularzu.

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

Uwagi

Ta kolekcja ma określony atrybut, jeśli określony typ atrybutu istnieje w kolekcji, a wartość określonego atrybutu jest taka sama jak wartość wystąpienia atrybutu w kolekcji.

Różnica między metodami Matches i Contains polega na tym, że Matches wywołuje Match metodę w atrybucie i Contains wywołuje metodę Equals .

W przypadku większości atrybutów te metody wykonują to samo. Atrybuty, które mogą mieć wiele flag, są jednak zwykle implementowane tak, Match aby zwracały true , jeśli którakolwiek z flag jest spełniona. Rozważmy na przykład atrybut powiązania danych z flagami logicznymi "SupportsSql", "SupportsOleDb" i "SupportsXml". Ten atrybut może znajdować się we właściwości obsługującej wszystkie trzy podejścia powiązania danych. Często zdarza się, że programista musi wiedzieć tylko wtedy, gdy określone podejście jest dostępne, a nie wszystkie trzy. W związku z tym programista może używać z Match wystąpieniem atrybutu zawierającego tylko flagi wymagane przez programistę.

Zobacz też

Dotyczy

Contains(Attribute[])

Źródło:
AttributeCollection.cs
Źródło:
AttributeCollection.cs
Źródło:
AttributeCollection.cs

Określa, czy ta kolekcja atrybutów zawiera wszystkie określone atrybuty w tablicy atrybutów.

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

Parametry

attributes
Attribute[]

Tablica typu Attribute do znalezienia w kolekcji.

Zwraca

true jeśli kolekcja zawiera wszystkie atrybuty; w przeciwnym razie , false.

Przykłady

Poniższy przykład kodu porównuje atrybuty w button1 obiekcie i textBox1 w celu sprawdzenia, czy atrybuty przycisku znajdują się w atrybutach pola tekstowego. Przyjęto założenie, że formularz button1 i textBox1 zostały utworzone w formularzu.

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

Uwagi

Ta kolekcja ma określoną tablicę atrybutów, jeśli wszystkie określone typy atrybutów istnieją w kolekcji, a każdy atrybut w określonej tablicy jest taki sam jak atrybut w kolekcji.

Zobacz też

Dotyczy