Export (0) Print
Expand All

Type.GetMember Method (String, MemberTypes, BindingFlags)

.NET Framework 1.1

Searches for the specified members of the specified member type, using the specified binding constraints.

[Visual Basic]
Overloads Public Overridable Function GetMember( _
   ByVal name As String, _
   ByVal type As MemberTypes, _
   ByVal bindingAttr As BindingFlags _
) As MemberInfo()
[C#]
public virtual MemberInfo[] GetMember(
 string name,
 MemberTypes type,
 BindingFlags bindingAttr
);
[C++]
public: virtual MemberInfo* GetMember(
 String* name,
 MemberTypes type,
 BindingFlags bindingAttr
) [];
[JScript]
public function GetMember(
   name : String,
 type : MemberTypes,
 bindingAttr : BindingFlags
) : MemberInfo[];

Parameters

name
The String containing the name of the members to get.
type
The MemberType to search for.
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 the public members with the specified name, if found; otherwise, a null reference (Nothing in Visual Basic).

Exceptions

Exception Type Condition
ArgumentNullException name is a null reference (Nothing in Visual Basic).
NotSupportedException A derived class must provide an implementation.

Remarks

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

This method searches the current classes and its base classes. 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.IgnoreCase to ignore the case of name.
  • BindingFlags.DeclaredOnly to search only the members declared on the Type, not members that were simply inherited.

See System.Reflection.BindingFlags for more information.

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

Example

[Visual Basic, C#, C++] The following example displays all the methods of the myString class that start with the letter C.

[Visual Basic] 
Public Sub GetPublicInstanceMethodMemberInfo()
    Dim myString As [String] = "GetMember_String_MemberType_BindingFlag"

    Dim myType As Type = myString.GetType()
    ' Get the public instance methods for myString starting with the letter C.
    Dim myMembers As MemberInfo() = myType.GetMember("C*", MemberTypes.Method, BindingFlags.Public Or BindingFlags.Instance)
    If myMembers.Length > 0 Then
        Console.WriteLine(ControlChars.Cr + "The public instance method(s) starting with the letter C for type {0}:", myType)
        Dim index As Integer
        For index = 0 To myMembers.Length - 1
            Console.WriteLine("Member {0}: {1}", index + 1, myMembers(index).ToString())
        Next index
    Else
        Console.WriteLine("No members match the search criteria.")
    End If
End Sub 'GetPublicInstanceMethodMemberInfo 
End Class 'MyMemberSample

[C#] 
public void GetPublicInstanceMethodMemberInfo()
{
    String myString = "GetMember_String_MemberType_BindingFlag";
    Type myType = myString.GetType();
    // Get the public instance methods for myString starting with the letter C.
    MemberInfo[] myMembers = myType.GetMember("C*", MemberTypes.Method, 
        BindingFlags.Public | BindingFlags.Instance);
    if(myMembers.Length > 0)
    {
        Console.WriteLine("\nThe public instance method(s) starting with the letter C for type {0}:", myType);
        for(int index=0; index < myMembers.Length; index++)
            Console.WriteLine("Member {0}: {1}", index + 1, myMembers[index].ToString());
    }
    else
        Console.WriteLine("No members match the search criteria.");    
}
}

[C++] 
void GetPublicInstanceMethodMemberInfo() {
   String*  myString = S"GetMember_String_MemberType_BindingFlag";
   Type*  myType = myString->GetType();
   // Get the public instance methods for myString starting with the letter C.
   MemberInfo*  myMembers[] = myType->GetMember(S"C*", MemberTypes::Method,
      static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance));
   if (myMembers->Length > 0) {
      Console::WriteLine(S"\nThe public instance method(s) starting with the letter C for type {0}:", myType);
      for (int index=0; index < myMembers->Length; index++)
         Console::WriteLine(S"Member {0}: {1}",__box( index + 1), myMembers->Item[index]);
   } else
      Console::WriteLine(S"No members match the search criteria.");
}

[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 Framework Security: 

See Also

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

Show:
© 2014 Microsoft