Type.GetDefaultMembers Metodo
Definizione
Cerca i membri definiti per l'oggetto Type corrente per cui è impostato il DefaultMemberAttribute.Searches for the members defined for the current Type whose DefaultMemberAttribute is set.
public:
virtual cli::array <System::Reflection::MemberInfo ^> ^ GetDefaultMembers();
public virtual System.Reflection.MemberInfo[] GetDefaultMembers ();
abstract member GetDefaultMembers : unit -> System.Reflection.MemberInfo[]
override this.GetDefaultMembers : unit -> System.Reflection.MemberInfo[]
Public Overridable Function GetDefaultMembers () As MemberInfo()
Restituisce
Matrice di oggetti MemberInfo che rappresentano tutti i membri predefiniti dell'oggetto Type corrente.An array of MemberInfo objects representing all default members of the current Type.
-oppure--or- Matrice vuota di tipo MemberInfo, se per l'oggetto Type corrente non esistono membri predefiniti.An empty array of type MemberInfo, if the current Type does not have default members.
Implementazioni
Esempio
Nell'esempio seguente vengono ottenute le informazioni sui membri predefinite di MyClass
e vengono visualizzati i membri predefiniti.The following example obtains the default member information of MyClass
and displays the default members.
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
Il GetDefaultMembers metodo non restituisce membri in un ordine particolare, ad esempio in ordine alfabetico o di dichiarazione.The GetDefaultMembers method does not return members in a particular order, such as alphabetical or declaration order. Il codice non deve dipendere dall'ordine in cui i membri vengono restituiti, perché questo ordine varia.Your code must not depend on the order in which members are returned, because that order varies.
Questo metodo può essere sottoposto a override da una classe derivata.This method can be overridden by a derived class.
I membri includono proprietà, metodi, campi, eventi e così via.Members include properties, methods, fields, events, and so on.
La tabella seguente mostra quali membri di una classe di base vengono restituiti dai Get
metodi durante la reflection su un tipo.The following table shows what members of a base class are returned by the Get
methods when reflecting on a type.
Tipo di membroMember Type | StaticStatic | Non staticoNon-Static |
---|---|---|
CostruttoreConstructor | NoNo | NoNo |
CampoField | NoNo | Sì.Yes. Un campo è sempre nascosto per nome e firma.A field is always hide-by-name-and-signature. |
EventoEvent | Non applicabileNot applicable | La regola Common Type System è che l'ereditarietà è identica a quella dei metodi che implementano la proprietà.The common type system rule is that the inheritance is the same as that of the methods that implement the property. La reflection considera le proprietà come nascoste per nome e firma.Reflection treats properties as hide-by-name-and-signature. Vedere la nota 2 di seguito.See note 2 below. |
MetodoMethod | NoNo | Sì.Yes. Un metodo (sia virtuale che non virtuale) può essere nascosto in base al nome o nascosto per nome e firma.A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature. |
Tipo annidatoNested Type | NoNo | NoNo |
ProprietàProperty | Non applicabileNot applicable | La regola Common Type System è che l'ereditarietà è identica a quella dei metodi che implementano la proprietà.The common type system rule is that the inheritance is the same as that of the methods that implement the property. La reflection considera le proprietà come nascoste per nome e firma.Reflection treats properties as hide-by-name-and-signature. Vedere la nota 2 di seguito.See note 2 below. |
Hide-by-Name-and-signature prende in considerazione tutte le parti della firma, inclusi i modificatori personalizzati, i tipi restituiti, i tipi di parametro, le sentinelle e le convenzioni di chiamata non gestite.Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. Si tratta di un confronto binario.This is a binary comparison.
Per la reflection, le proprietà e gli eventi sono nascosti per nome e firma.For reflection, properties and events are hide-by-name-and-signature. Se si dispone di una proprietà con una funzione di accesso get e set nella classe di base, ma la classe derivata dispone solo di una funzione di accesso get, la proprietà della classe derivata nasconde la proprietà della classe base e non sarà possibile accedere al setter sulla classe di base.If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.
Gli attributi personalizzati non fanno parte del Common Type System.Custom attributes are not part of the common type system.
Se l'oggetto corrente Type rappresenta un tipo generico costruito, questo metodo restituisce gli MemberInfo oggetti con i parametri di tipo sostituiti dagli argomenti di tipo appropriati.If the current Type represents a constructed generic type, this method returns the MemberInfo objects with the type parameters replaced by the appropriate type arguments. Se, ad esempio, C<T>
la classe ha una proprietà P
che restituisce T
, la chiamata a GetDefaultMembers C<int>
restituisce int P
in C# ( Property P As Integer
in Visual Basic).For example, if class C<T>
has a property P
that returns T
, calling GetDefaultMembers on C<int>
returns int P
in C# (Property P As Integer
in Visual Basic).
Se l'oggetto corrente Type rappresenta un parametro di tipo nella definizione di un tipo o di un metodo generico, questo metodo cerca i membri del vincolo di classe o i membri di Object se non è presente alcun vincolo di classe.If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the members of the class constraint, or the members of Object if there is no class constraint.