DefaultMemberAttribute Classe
Definizione
Definisce il membro di un tipo che è il membro predefinito utilizzato dal metodo InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]).Defines the member of a type that is the default member used by 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
- Ereditarietà
- Attributi
Esempio
Nell'esempio seguente viene usato l' DefaultMemberAttribute attributo per rendere la Age
proprietà il membro predefinito della MyClass
classe.The following example uses the DefaultMemberAttribute attribute to make the Age
property the default member of the MyClass
class.
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
Commenti
Una proprietà viene importata come indicizzatore (proprietà indicizzata predefinita in Visual Basic) se la proprietà dispone di argomenti e se il nome della proprietà o una delle relative funzioni di accesso corrisponde al nome specificato da DefaultMemberAttribute .A property is imported as an indexer (default indexed property in Visual Basic) if the property has arguments and if the name of the property or one of its accessors matches the name specified by the DefaultMemberAttribute. Se DefaultMemberAttribute non è presente nel tipo che lo contiene, il tipo non ha un indicizzatore.If the DefaultMemberAttribute is not present on the containing type, then the type does not have an indexer. Il compilatore C# genera l'oggetto DefaultMemberAttribute su qualsiasi tipo contenente un indicizzatore.The C# compiler emits the DefaultMemberAttribute on any type containing an indexer. In C# è un errore impostare manualmente un tipo con l'attributo DefaultMemberAttribute se il tipo dichiara anche un indicizzatore.In C# it is an error to manually attribute a type with the DefaultMemberAttribute if the type also declares an indexer.
Costruttori
DefaultMemberAttribute(String) |
Inizializza una nuova istanza della classe DefaultMemberAttribute.Initializes a new instance of the DefaultMemberAttribute class. |
Proprietà
MemberName |
Ottiene il nome dall'attributo.Gets the name from the attribute. |
TypeId |
Quando è implementata in una classe derivata, ottiene un identificatore univoco della classe Attribute.When implemented in a derived class, gets a unique identifier for this Attribute. (Ereditato da Attribute) |
Metodi
Equals(Object) |
Restituisce un valore che indica se questa istanza è uguale a un oggetto specificato.Returns a value that indicates whether this instance is equal to a specified object. (Ereditato da Attribute) |
GetHashCode() |
Restituisce il codice hash per l'istanza.Returns the hash code for this instance. (Ereditato da Attribute) |
GetType() |
Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance. (Ereditato da Object) |
IsDefaultAttribute() |
In caso di override in una classe derivata, indica se il valore di questa istanza è il valore predefinito per la classe derivata.When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Ereditato da Attribute) |
Match(Object) |
Quando è sottoposto a override in una classe derivata, restituisce un valore che indica se questa istanza equivale a un oggetto specificato.When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Ereditato da Attribute) |
MemberwiseClone() |
Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object. (Ereditato da Object) |
ToString() |
Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object. (Ereditato da Object) |
Implementazioni dell'interfaccia esplicita
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Esegue il mapping di un set di nomi a un set corrispondente di ID dispatch.Maps a set of names to a corresponding set of dispatch identifiers. (Ereditato da Attribute) |
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Recupera le informazioni sul tipo relative a un oggetto, che possono essere usate per ottenere informazioni sul tipo relative a un'interfaccia.Retrieves the type information for an object, which can be used to get the type information for an interface. (Ereditato da Attribute) |
_Attribute.GetTypeInfoCount(UInt32) |
Recupera il numero delle interfacce di informazioni sul tipo fornite da un oggetto (0 o 1).Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Ereditato da Attribute) |
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Fornisce l'accesso a proprietà e metodi esposti da un oggetto.Provides access to properties and methods exposed by an object. (Ereditato da Attribute) |