GetDefaultMembers Méthode
TOC
Réduire la table des matières
Développer la table des matières
Cet article a fait l’objet d’une traduction automatique. Pour afficher l’article en anglais, activez la case d’option Anglais. Vous pouvez également afficher le texte anglais dans une fenêtre contextuelle en faisant glisser le pointeur de la souris sur le texte traduit.
Traduction
Anglais

Type::GetDefaultMembers, méthode ()

 

Recherche les membres définis pour le Type actuel dont le DefaultMemberAttribute est défini.

Espace de noms:   System
Assembly:  mscorlib (dans mscorlib.dll)

public:
virtual array<MemberInfo^>^ GetDefaultMembers()

Valeur de retour

Type: array<System.Reflection::MemberInfo^>^

Tableau d'objets MemberInfo représentant tous les membres par défaut du Type actuel.

ou

Tableau vide du type MemberInfo si le Type actuel n'a pas de membres par défaut.

The M:System.Type.GetDefaultMembers method does not return members in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which members are returned, because that order varies.

This method can be overridden by a derived class.

Members include properties, methods, fields, events, and so on.

The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

Member Type

Static

Non-Static

Constructor

No

No

Field

No

Yes. A field is always hide-by-name-and-signature.

Event

Not applicable

The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below.

Method

No

Yes. A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.

Nested Type

No

No

Property

Not applicable

The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below.

  1. 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. This is a binary comparison.

  2. For reflection, properties and events are hide-by-name-and-signature. 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.

  3. Custom attributes are not part of the common type system.

If the current T:System.Type represents a constructed generic type, this method returns the T:System.Reflection.MemberInfo objects with the type parameters replaced by the appropriate type arguments. For example, if class C<T> has a property P that returns T, calling M:System.Type.GetDefaultMembers on C<int> returns int P in C# (Property P As Integer in Visual Basic).

If the current T:System.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 T:System.Object if there is no class constraint.

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 );
   }
}

.NET Framework
Disponible depuis 1.1
Portable Class Library
Pris en charge dans : portable .NET platforms
Silverlight
Disponible depuis 2.0
Windows Phone Silverlight
Disponible depuis 7.0
Retour au début
Afficher:
© 2016 Microsoft