AttributeCollection.Contains Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Determina se esta coleção de atributos tem o atributo especificado ou um array de atributos.
Sobrecargas
| Name | Description |
|---|---|
| Contains(Attribute) |
Determina se esta coleção de atributos possui o atributo especificado. |
| Contains(Attribute[]) |
Determina se esta coleção de atributos contém todos os atributos especificados no array de atributos. |
Contains(Attribute)
Determina se esta coleção de atributos possui o atributo especificado.
public:
bool Contains(Attribute ^ attribute);
public bool Contains(Attribute attribute);
member this.Contains : Attribute -> bool
Public Function Contains (attribute As Attribute) As Boolean
Parâmetros
Devoluções
true se a coleção contiver o atributo ou for o atributo padrão para o tipo de atributo; caso contrário, false.
Exemplos
O exemplo de código seguinte verifica se a coleção tem um BrowsableAttribute conjunto de true. Assume que button1 e textBox1 foram criados numa forma.
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
Observações
Esta coleção tem o atributo especificado se o tipo de atributo especificado existir na coleção, e se o valor do atributo especificado for o mesmo que o valor da instância do atributo na coleção.
A diferença entre os Matches métodos e Contains é que Matches chama o Match método num atributo, e Contains chama o Equals método.
Para a maioria dos atributos, estes métodos fazem o mesmo. Para atributos que podem ter múltiplas flags, no entanto, Match é normalmente implementado de modo a que retorne true se alguma das flags for satisfeita. Por exemplo, considere um atributo de ligação de dados com as flags booleanas "SupportsSql", "SupportsOleDb" e "SupportsXml". Este atributo pode estar presente numa propriedade que suporta as três abordagens de ligação de dados. Muitas vezes, um programador só precisa de saber se existe uma abordagem específica, não as três. Portanto, um programador pode usar Match com uma instância do atributo contendo apenas as flags de que o programador necessita.
Ver também
Aplica-se a
Contains(Attribute[])
Determina se esta coleção de atributos contém todos os atributos especificados no array de atributos.
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
Parâmetros
Devoluções
true se a coleção contiver todos os atributos; caso contrário, false.
Exemplos
O exemplo de código seguinte compara os atributos em button1 e textBox1 para ver se os atributos do botão estão contidos nos atributos da caixa de texto. Assume que tanto button1 como textBox1 foram criados numa forma.
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
Observações
Esta coleção tem o array especificado de atributos se todos os tipos de atributos especificados existirem na coleção e se cada atributo no array especificado for igual a um atributo na coleção.