AttributeCollection.Matches Metoda

Definicja

Określa, czy określony atrybut lub tablica atrybutów jest taki sam jak atrybut lub tablica atrybutów w kolekcji.

Przeciążenia

Matches(Attribute)

Określa, czy określony atrybut jest taki sam jak atrybut w kolekcji.

Matches(Attribute[])

Określa, czy atrybuty w określonej tablicy są takie same jak atrybuty w kolekcji.

Matches(Attribute)

Określa, czy określony atrybut jest taki sam jak atrybut w kolekcji.

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

Parametry

attribute
Attribute

Wystąpienie klasy Attribute do porównania z atrybutami w tej kolekcji.

Zwraca

Boolean

true jeśli atrybut jest zawarty w kolekcji i ma taką samą wartość jak atrybut w kolekcji; w przeciwnym razie , false.

Przykłady

Poniższy przykład kodu sprawdza, czy BrowsableAttribute element jest członkiem kolekcji i czy został ustawiony na truewartość . Przyjęto założenie, że button1 formularz i textBox1 zostały utworzone w formularzu.

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

Uwagi

Atrybut może zapewnić obsługę dopasowywania.

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

Matches(Attribute[])

Określa, czy atrybuty w określonej tablicy są takie same jak atrybuty w kolekcji.

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

Parametry

attributes
Attribute[]

Tablica, która ma być porównywana MemberAttributes z atrybutami w tej kolekcji.

Zwraca

Boolean

true jeśli wszystkie atrybuty w tablicy znajdują się w kolekcji i mają te same wartości co atrybuty w kolekcji; w przeciwnym razie , false.

Przykłady

Poniższy przykład kodu porównuje atrybuty w przycisku i polu tekstowym, aby sprawdzić, czy są one zgodne. Przyjęto założenie, że button1 formularz i textBox1 zostały utworzone w formularzu.

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

Uwagi

Atrybut może zapewnić obsługę dopasowywania.

Zobacz też

Dotyczy