AttributeCollection.Contains Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Avgör om den här samlingen av attribut har det angivna attributet eller matrisen med attribut.
Överlagringar
| Name | Description |
|---|---|
| Contains(Attribute) |
Avgör om den här samlingen av attribut har det angivna attributet. |
| Contains(Attribute[]) |
Avgör om den här attributsamlingen innehåller alla angivna attribut i attributmatrisen. |
Contains(Attribute)
Avgör om den här samlingen av attribut har det angivna attributet.
public:
bool Contains(Attribute ^ attribute);
public bool Contains(Attribute attribute);
member this.Contains : Attribute -> bool
Public Function Contains (attribute As Attribute) As Boolean
Parametrar
Returer
trueom samlingen innehåller attributet eller är standardattributet för typen av attribut; annars . false
Exempel
Följande kodexempel kontrollerar om samlingen har värdet BrowsableAttributetrue. Det förutsätter att button1 och textBox1 har skapats i ett formulär.
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
Kommentarer
Den här samlingen har det angivna attributet om den angivna typen av attribut finns i samlingen, och om värdet för det angivna attributet är detsamma som värdet för instansen av attributet i samlingen.
Skillnaden mellan Matches metoderna och Contains är att Matches anropa Match metoden för ett attribut och Contains anropar Equals metoden.
För de flesta attribut gör dessa metoder samma sak. För attribut som kan ha flera flaggor implementeras dock Match vanligtvis så att de returnerar true om någon av flaggorna är uppfyllda. Överväg till exempel ett databindningsattribut med booleska flaggorna "SupportsSql", "SupportsOleDb" och "SupportsXml". Det här attributet kan finnas på en egenskap som stöder alla tre databindningsmetoderna. Det är ofta så att en programmerare bara behöver veta om en viss metod är tillgänglig, inte alla tre. Därför kan en programmerare använda Match med en instans av attributet som endast innehåller de flaggor som programmeraren behöver.
Se även
Gäller för
Contains(Attribute[])
Avgör om den här attributsamlingen innehåller alla angivna attribut i attributmatrisen.
public:
bool Contains(cli::array <Attribute ^> ^ attributes);
public bool Contains(Attribute[] attributes);
member this.Contains : Attribute[] -> bool
Public Function Contains (attributes As Attribute()) As Boolean
Parametrar
Returer
trueom samlingen innehåller alla attribut; annars . false
Exempel
I följande kodexempel jämförs attributen i button1 och textBox1 för att se om attributen för knappen finns i attributen för textrutan. Det förutsätter att både button1 och textBox1 har skapats i ett formulär.
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
Kommentarer
Den här samlingen har den angivna matrisen med attribut om alla angivna attributtyper finns i samlingen och om varje attribut i den angivna matrisen är samma som ett attribut i samlingen.