Type.GetDefaultMembers 方法

定义

搜索为设置了 Type 的当前 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()

返回

表示当前 MemberInfo 的所有默认成员的 Type 对象数组。An array of MemberInfo objects representing all default members of the current Type.

- 或 --or- 如果当前 MemberInfo 没有默认成员,则为 Type 类型的空数组。An empty array of type MemberInfo, if the current Type does not have default members.

实现

示例

下面的示例获取的默认成员信息MyClass并显示默认成员。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

注解

GetDefaultMembers方法不按特定顺序(如字母顺序或声明顺序)返回成员。The 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.

下表显示了在类型上反射时Get方法返回的基类成员。The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成员类型Member Type StaticStatic 非静态Non-Static
构造函数Constructor NoNo NoNo
字段Field NoNo 可以。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. 请参阅下面的注释2。See note 2 below.
方法Method NoNo 可以。Yes. 方法(虚拟和非虚拟)可按名称隐藏或按名称和签名隐藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
嵌套类型Nested Type NoNo NoNo
PropertyProperty 不适用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. 请参阅下面的注释2。See note 2 below.
  1. 按名称和签名隐藏将考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、个 sentinel 和非托管调用约定。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. 如果在基类中同时具有 get 访问器和 set 访问器的属性,但派生类只有 get 访问器,则派生类属性将隐藏基类属性,并且你将无法访问基类的资源库。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.

如果当前Type表示构造泛型类型,则此方法MemberInfo返回类型参数替换为相应类型参数的对象。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. 例如,如果类C<T>具有一个返回P T的属性,则在GetDefaultMembersC<int>调用int P将C#在Property P As Integer中返回(在 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).

如果当前Type表示泛型类型或泛型方法的定义中的类型参数,则此方法会搜索类约束的成员,或者Object如果没有类约束,则搜索的成员。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.

适用于

另请参阅