Type.GetMember Method (String, BindingFlags)
Searches for the specified members, using the specified binding constraints.
[Visual Basic] Overloads Public Overridable Function GetMember( _ ByVal name As String, _ ByVal bindingAttr As BindingFlags _ ) As MemberInfo() Implements IReflect.GetMember [C#] public virtual MemberInfo[] GetMember( string name, BindingFlags bindingAttr ); [C++] public: virtual MemberInfo* GetMember( String* name, BindingFlags bindingAttr ) []; [JScript] public function GetMember( name : String, bindingAttr : BindingFlags ) : MemberInfo[];
Parameters
- name
- The String containing the name of the members to get.
- 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).
Implements
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentNullException | name is a null reference (Nothing in Visual Basic). |
Remarks
This method can be overridden by a derived class.
Members include properties, methods, fields, events, and so on.
This method searches the current classes and its base classes. If the requested member 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:
- Instance to include instance members in the search.
- Static to include static members in the search.
- Public to include public members in the search.
- NonPublic to include non-public members (that is, private and protected members) in the search.
The following BindingFlags modifier flags can be used to change how the search works:
- IgnoreCase to ignore the case of name.
- 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 public static members of the myString class that start with the letter C.
[Visual Basic] Public Sub GetPublicStaticMemberInfo() Dim myString As [String] = "GetMember_String_BindingFlag" Dim myType As Type = myString.GetType() ' Get the public static members for the class myString starting with the letter C. Dim myMembers As MemberInfo() = myType.GetMember("C*", BindingFlags.Public Or BindingFlags.Static) If myMembers.Length > 0 Then Console.WriteLine(ControlChars.Cr + "The public static member(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 'GetPublicStaticMemberInfo [C#] public void GetPublicStaticMemberInfo() { String myString = "GetMember_String_BindingFlag"; Type myType = myString.GetType(); // Get the public static members for the class myString starting with the letter C. MemberInfo[] myMembers = myType.GetMember("C*", BindingFlags.Public |BindingFlags.Static); if(myMembers.Length > 0) { Console.WriteLine("\nThe public static member(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 GetPublicStaticMemberInfo() { String* myString = S"GetMember_String_BindingFlag"; Type* myType = myString->GetType(); // Get the public static members for the class myString starting with the letter C MemberInfo* myMembers[] = myType->GetMember(S"C*", static_cast<BindingFlags>(BindingFlags::Public |BindingFlags::Static)); if (myMembers->Length > 0) { Console::WriteLine(S"\nThe public static member(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
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:
- ReflectionPermission for reflecting non-public objects. Associated enumeration: ReflectionPermissionFlag.TypeInformation
See Also
Type Class | Type Members | System Namespace | Type.GetMember Overload List | MemberInfo | String | BindingFlags | DefaultBinder | ReflectionPermission | GetMembers | GetDefaultMembers | FindMembers