DefaultMemberAttribute Classe
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Define o membro de um tipo que é o membro padrão usado por InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]).
public ref class DefaultMemberAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct)]
public sealed class DefaultMemberAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct)]
[System.Serializable]
public sealed class DefaultMemberAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class DefaultMemberAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct)>]
type DefaultMemberAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct)>]
[<System.Serializable>]
type DefaultMemberAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type DefaultMemberAttribute = class
inherit Attribute
Public NotInheritable Class DefaultMemberAttribute
Inherits Attribute
- Herança
- Atributos
O exemplo a seguir usa o DefaultMemberAttribute atributo para tornar a Age
propriedade o membro padrão da MyClass
classe .
using namespace System;
using namespace System::Reflection;
using namespace System::IO;
[DefaultMemberAttribute("Age")]
public ref class MyClass
{
public:
void Name( String^ s ){}
property int Age
{
int get()
{
return 20;
}
}
};
int main()
{
try
{
Type^ myType = MyClass::typeid;
array<MemberInfo^>^memberInfoArray = myType->GetDefaultMembers();
if ( memberInfoArray->Length > 0 )
{
System::Collections::IEnumerator^ myEnum = memberInfoArray->GetEnumerator();
while ( myEnum->MoveNext() )
{
MemberInfo^ memberInfoObj = safe_cast<MemberInfo^>(myEnum->Current);
Console::WriteLine( "The default member name is: {0}", memberInfoObj );
}
}
else
{
Console::WriteLine( "No default members are available." );
}
}
catch ( InvalidOperationException^ e )
{
Console::WriteLine( "InvalidOperationException: {0}", e->Message );
}
catch ( IOException^ e )
{
Console::WriteLine( "IOException: {0}", e->Message );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception: {0}", e->Message );
}
}
using System;
using System.Reflection;
using System.IO;
[DefaultMemberAttribute("Age")]
public class MyClass
{
public void Name(String s) {}
public int Age
{
get
{
return 20;
}
}
public static void Main()
{
try
{
Type myType = typeof(MyClass);
MemberInfo[] memberInfoArray = myType.GetDefaultMembers();
if (memberInfoArray.Length > 0)
{
foreach(MemberInfo memberInfoObj in memberInfoArray)
{
Console.WriteLine("The default member name is: " + memberInfoObj.ToString());
}
}
else
{
Console.WriteLine("No default members are available.");
}
}
catch(InvalidOperationException e)
{
Console.WriteLine("InvalidOperationException: " + e.Message);
}
catch(IOException e)
{
Console.WriteLine("IOException: " + e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
}
Imports System.Reflection
Imports System.IO
<DefaultMemberAttribute("Age")> Public Class [MyClass]
Public Sub Name(ByVal s As String)
End Sub
Public ReadOnly Property Age() As Integer
Get
Return 20
End Get
End Property
Public Shared Sub Main()
Try
Dim myType As Type = GetType([MyClass])
Dim memberInfoArray As MemberInfo() = myType.GetDefaultMembers()
If memberInfoArray.Length > 0 Then
Dim memberInfoObj As MemberInfo
For Each memberInfoObj In memberInfoArray
Console.WriteLine("The default member name is: " + memberInfoObj.ToString())
Next memberInfoObj
Else
Console.WriteLine("No default members are available.")
End If
Catch e As InvalidOperationException
Console.WriteLine("InvalidOperationException: " + e.Message)
Catch e As IOException
Console.WriteLine("IOException: " + e.Message)
Catch e As Exception
Console.WriteLine("Exception: " + e.Message)
End Try
End Sub
End Class
Uma propriedade será importada como um indexador (propriedade indexada padrão no Visual Basic) se a propriedade tiver argumentos e se o nome da propriedade ou um de seus acessadores corresponder ao nome especificado pelo DefaultMemberAttribute. Se o DefaultMemberAttribute não estiver presente no tipo recipiente, o tipo não terá um indexador. O compilador C# emite o DefaultMemberAttribute em qualquer tipo que contenha um indexador. No C#, é um erro atribuir manualmente um tipo com o DefaultMemberAttribute se o tipo também declarar um indexador.
Default |
Inicializa uma nova instância da classe DefaultMemberAttribute. |
Member |
Obtém o nome do atributo. |
Type |
Quando implementado em uma classe derivada, obtém um identificador exclusivo para este Attribute. (Herdado de Attribute) |
Equals(Object) |
Retorna um valor que indica se essa instância é igual a um objeto especificado. (Herdado de Attribute) |
Get |
Retorna o código hash para a instância. (Herdado de Attribute) |
Get |
Obtém o Type da instância atual. (Herdado de Object) |
Is |
Quando substituído em uma classe derivada, indica se o valor dessa instância é o valor padrão para a classe derivada. (Herdado de Attribute) |
Match(Object) |
Quando substituído em uma classe derivada, retorna um valor que indica se essa instância é igual a um objeto especificado. (Herdado de Attribute) |
Memberwise |
Cria uma cópia superficial do Object atual. (Herdado de Object) |
To |
Retorna uma cadeia de caracteres que representa o objeto atual. (Herdado de Object) |
_Attribute. |
Mapeia um conjunto de nomes para um conjunto correspondente de identificadores de expedição. (Herdado de Attribute) |
_Attribute. |
Recupera as informações de tipo para um objeto, que pode ser usado para obter as informações de tipo para uma interface. (Herdado de Attribute) |
_Attribute. |
Retorna o número de interfaces de informações do tipo que um objeto fornece (0 ou 1). (Herdado de Attribute) |
_Attribute. |
Fornece acesso a propriedades e métodos expostos por um objeto. (Herdado de Attribute) |
Produto | Versões |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Comentários do .NET
O .NET é um projeto código aberto. Selecione um link para fornecer comentários: