Export (0) Print
Expand All

Type.GetMembers Method (BindingFlags)

.NET Framework 1.1

When overridden in a derived class, searches for the members defined for the current Type, using the specified binding constraints.

[Visual Basic]
Overloads Public MustOverride Function GetMembers( _
   ByVal bindingAttr As BindingFlags _
) As MemberInfo() Implements IReflect.GetMembers
[C#]
public abstract MemberInfo[] GetMembers(
 BindingFlags bindingAttr
);
[C++]
public: virtual MemberInfo* GetMembers(
 BindingFlags bindingAttr
) [] = 0;
[JScript]
public abstract function GetMembers(
   bindingAttr : BindingFlags
) : MemberInfo[];

Parameters

bindingAttr
A bitmask comprised of one or more BindingFlags that specify how the search is conducted.

-or-

Zero, to return a null reference (Nothing in Visual Basic).

Return Value

An array of MemberInfo objects representing all members defined for the current Type that match the specified binding constraints.

-or-

An empty array of type MemberInfo, if no members are defined for the current Type, or if none of the defined members match the binding constraints.

Implements

IReflect.GetMembers

Remarks

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

If the requested type is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference (Nothing in Visual Basic).

The following BindingFlags filter flags can be used to define which members to include in the search:

  • You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.
  • Specify BindingFlags.Public to include public members in the search.
  • Specify BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.
  • Specify BindingFlags.FlattenHierarchy to include static members up the hierarchy.

The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.DeclaredOnly to search only the members declared on the Type, not members that were simply inherited.

Calling this method with only the Public flag or only the NonPublic flag will return the specified members and does not require any other flags.

See System.Reflection.BindingFlags for more information.

Class initializers are available through GetMember, GetMembers, FindMembers, and GetConstructors.

Example

[Visual Basic] 
Class [MyClass]
   Public myInt As Integer = 0
   Public myString As String = Nothing
   
   
   Public Sub New()
   End Sub 'New
   
   Public Sub Myfunction()
   End Sub 'Myfunction
End Class '[MyClass]

Class Type_GetMembers_BindingFlags
   
   Public Shared Sub Main()
      Try
         Dim MyObject As New [MyClass]()
         Dim myMemberInfo() As MemberInfo
         
         ' Get the type of the class 'MyClass'.
         Dim myType As Type = MyObject.GetType()
         
         ' Get the public instance members of the class 'MyClass'. 
         myMemberInfo = myType.GetMembers((BindingFlags.Public Or BindingFlags.Instance))
         
         Console.WriteLine(ControlChars.Cr + "The public instance members of class '{0}' are : " + ControlChars.Cr, myType)
         Dim i As Integer
         For i = 0 To myMemberInfo.Length - 1
            ' Display name and type of the member of 'MyClass'.
            Console.WriteLine("'{0}' is a {1}", myMemberInfo(i).Name, myMemberInfo(i).MemberType)
         Next i
      
      Catch e As SecurityException
         Console.WriteLine(("SecurityException : " + e.Message.ToString()))
      End Try
   End Sub 'Main 
End Class 'Type_GetMembers_BindingFlags

[C#] 

class MyClass
{
   public int myInt = 0;
   public string myString = null;

   public MyClass()
   {
   }
   public void Myfunction()
   {
   }
}

class Type_GetMembers_BindingFlags
{
   public static void Main()
   {
      try
      {
         MyClass MyObject = new MyClass();
         MemberInfo [] myMemberInfo; 

         // Get the type of the class 'MyClass'.
         Type myType = MyObject.GetType(); 
        
         // Get the public instance members of the class 'MyClass'. 
         myMemberInfo = myType.GetMembers(BindingFlags.Public|BindingFlags.Instance);
    
         Console.WriteLine( "\nThe public instance members of class '{0}' are : \n", myType); 
         for (int i =0 ; i < myMemberInfo.Length ; i++)
         {
            // Display name and type of the member of 'MyClass'.
            Console.WriteLine( "'{0}' is a {1}", myMemberInfo[i].Name, myMemberInfo[i].MemberType);
         }

      }
      catch (SecurityException e)
      {
         Console.WriteLine("SecurityException : " + e.Message ); 
      }      
   }
}


[C++] 

__gc class MyClass {
public:
    int*  myInt;
    String* myString;
    MyClass() {
    }
    void Myfunction() {
    }
};

int main() {
    try {
        MyClass* MyObject = new MyClass();
        MemberInfo* myMemberInfo[];

        // Get the type of the class 'MyClass'.
        Type*  myType = MyObject->GetType();

        // Get the public instance members of the class 'MyClass'.
        myMemberInfo = myType->GetMembers(static_cast<BindingFlags>(BindingFlags::Public|BindingFlags::Instance));

        Console::WriteLine(S"\nThe public instance members of class '{0}' are : \n", myType);
        for (int i =0 ; i < myMemberInfo->Length ; i++) {
            // Display name and type of the member of 'MyClass'.
            Console::WriteLine(S"'{0}' is a {1}", myMemberInfo[i]->Name,__box( myMemberInfo[i]->MemberType));
        }

    } catch (SecurityException* e) {
        Console::WriteLine(S"SecurityException : {0}", e->Message);
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Type Class | Type Members | System Namespace | Type.GetMembers Overload List | MemberInfo | BindingFlags | DefaultBinder | ReflectionPermission | GetMember | GetDefaultMembers | FindMembers

Show:
© 2014 Microsoft