Type.FindMembers Method
Returns a filtered array of MemberInfo objects of the specified member type.
[Visual Basic] Public Overridable Function FindMembers( _ ByVal memberType As MemberTypes, _ ByVal bindingAttr As BindingFlags, _ ByVal filter As MemberFilter, _ ByVal filterCriteria As Object _ ) As MemberInfo() [C#] public virtual MemberInfo[] FindMembers( MemberTypes memberType, BindingFlags bindingAttr, MemberFilter filter, object filterCriteria ); [C++] public: virtual MemberInfo* FindMembers( MemberTypes memberType, BindingFlags bindingAttr, MemberFilter* filter, Object* filterCriteria ) []; [JScript] public function FindMembers( memberType : MemberTypes, bindingAttr : BindingFlags, filter : MemberFilter, filterCriteria : Object ) : MemberInfo[];
Parameters
- memberType
- A MemberTypes object indicating the type of member 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).
- filter
- The delegate that does the comparisons, returning true if the member currently being inspected matches the filterCriteria and false otherwise. You can use the FilterAttribute, FilterName, and FilterNameIgnoreCase delegates supplied by this class. The first uses the fields of FieldAttributes, MethodAttributes, and MethodImplAttributes as search criteria, and the other two delegates use String objects as the search criteria.
- filterCriteria
- The search criteria that determines whether a member is returned in the array of MemberInfo objects.
The fields of FieldAttributes, MethodAttributes, and MethodImplAttributes can be used in conjunction with the FilterAttribute delegate supplied by this class.
Return Value
A filtered array of MemberInfo objects of the specified member type.
-or-
An empty array of type MemberInfo, if the current Type does not have members of type memberType that match the filter criteria.
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentNullException | filter 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. 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:
- 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:
- DeclaredOnly to search only the members declared on the Type, not members that were simply inherited.
See System.Reflection.BindingFlags for more information.
Valid values for MemberType are defined in MemberInfo. If no such members are found, an empty array is returned.
Class initializers are available through GetMember, GetMembers, FindMembers, and GetConstructors.
Example
[Visual Basic, C#, C++] The following example finds all the members in a class that match the specified search criteria, and then displays the matched members.
[Visual Basic] Imports System Imports System.Reflection Imports Microsoft.VisualBasic Class MyFindMembersClass Public Shared Sub Main() Dim objTest As New Object() Dim objType As Type = objTest.GetType() Dim arrayMemberInfo() As MemberInfo Try 'Find all static or public methods in the Object 'class that match the specified name. arrayMemberInfo = objType.FindMembers(MemberTypes.Method, _ BindingFlags.Public Or BindingFlags.Static _ Or BindingFlags.Instance, _ New MemberFilter(AddressOf DelegateToSearchCriteria), _ "ReferenceEquals") Dim index As Integer For index = 0 To arrayMemberInfo.Length - 1 Console.WriteLine("Result of FindMembers -" + ControlChars.Tab + _ arrayMemberInfo(index).ToString() + ControlChars.Cr) Next index Catch e As Exception Console.WriteLine("Exception : " + e.ToString()) End Try End Sub 'Main Public Shared Function DelegateToSearchCriteria _ (ByVal objMemberInfo As MemberInfo, _ ByVal objSearch As Object) As Boolean ' Compare the name of the member function with the filter criteria. If objMemberInfo.Name.ToString() = objSearch.ToString() Then Return True Else Return False End If End Function 'DelegateToSearchCriteria End Class 'MyFindMembersClass [C#] using System; using System.Reflection; class MyFindMembersClass { public static void Main() { Object objTest = new Object(); Type objType = objTest.GetType (); MemberInfo[] arrayMemberInfo; try { //Find all static or public methods in the Object class that match the specified name. arrayMemberInfo = objType.FindMembers(MemberTypes.Method, BindingFlags.Public | BindingFlags.Static| BindingFlags.Instance, new MemberFilter(DelegateToSearchCriteria), "ReferenceEquals"); for(int index=0;index < arrayMemberInfo.Length ;index++) Console.WriteLine ("Result of FindMembers -\t"+ arrayMemberInfo[index].ToString() +"\n"); } catch (Exception e) { Console.WriteLine ("Exception : " + e.ToString() ); } } public static bool DelegateToSearchCriteria(MemberInfo objMemberInfo, Object objSearch) { // Compare the name of the member function with the filter criteria. if(objMemberInfo.Name.ToString() == objSearch.ToString()) return true; else return false; } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::Reflection; __gc class MyFindMembersClass { public: static void Test() { Object* objTest = new Object(); Type* objType = objTest->GetType (); MemberInfo* arrayMemberInfo[]; try { //Find all static or public methods in the Object class that match the specified name. arrayMemberInfo = objType->FindMembers(MemberTypes::Method, static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Static| BindingFlags::Instance), new MemberFilter(0, DelegateToSearchCriteria), S"ReferenceEquals"); for (int index=0;index < arrayMemberInfo->Length ;index++) Console::WriteLine (S"Result of FindMembers -\t {0}", String::Concat( arrayMemberInfo[index], S"\n")); } catch (Exception* e) { Console::WriteLine (S"Exception : {0}", e); } } static bool DelegateToSearchCriteria(MemberInfo* objMemberInfo, Object* objSearch) { // Compare the name of the member function with the filter criteria. if (objMemberInfo->Name->Equals(objSearch->ToString())) return true; else return false; } }; int main() { MyFindMembersClass::Test(); }
[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 Framework Security:
- ReflectionPermission for reflecting non-public objects. Associated enumeration: ReflectionPermissionFlag.TypeInformation
See Also
Type Class | Type Members | System Namespace | MemberInfo | BindingFlags | DefaultBinder | ReflectionPermission | GetMember | GetMembers | GetDefaultMembers