Type.FindMembers(MemberTypes, BindingFlags, MemberFilter, Object) Metodo
Definizione
Restituisce una matrice filtrata di oggetti MemberInfo del tipo di membro specificato.Returns a filtered array of MemberInfo objects of the specified member type.
public:
virtual cli::array <System::Reflection::MemberInfo ^> ^ FindMembers(System::Reflection::MemberTypes memberType, System::Reflection::BindingFlags bindingAttr, System::Reflection::MemberFilter ^ filter, System::Object ^ filterCriteria);
public virtual System.Reflection.MemberInfo[]? FindMembers (System.Reflection.MemberTypes memberType, System.Reflection.BindingFlags bindingAttr, System.Reflection.MemberFilter filter, object filterCriteria);
public virtual System.Reflection.MemberInfo[] FindMembers (System.Reflection.MemberTypes memberType, System.Reflection.BindingFlags bindingAttr, System.Reflection.MemberFilter filter, object filterCriteria);
abstract member FindMembers : System.Reflection.MemberTypes * System.Reflection.BindingFlags * System.Reflection.MemberFilter * obj -> System.Reflection.MemberInfo[]
override this.FindMembers : System.Reflection.MemberTypes * System.Reflection.BindingFlags * System.Reflection.MemberFilter * obj -> System.Reflection.MemberInfo[]
Public Overridable Function FindMembers (memberType As MemberTypes, bindingAttr As BindingFlags, filter As MemberFilter, filterCriteria As Object) As MemberInfo()
Parametri
- memberType
- MemberTypes
Combinazione bit per bit di valori di enumerazione che indica il tipo di membro da cercare.A bitwise combination of the enumeration values that indicates the type of member to search for.
- bindingAttr
- BindingFlags
Combinazione bit per bit di valori di enumerazione che specifica il modo in cui viene eseguita la ricerca.A bitwise combination of the enumeration values that specify how the search is conducted.
-oppure--or-
Default per restituire null
.Default to return null
.
- filter
- MemberFilter
Delegato che esegue il confronto e restituisce true
e il membro in esame corrisponde a filterCriteria
; in caso contrario, false
.The delegate that does the comparisons, returning true
if the member currently being inspected matches the filterCriteria
and false
otherwise.
- filterCriteria
- Object
Criteri di ricerca che determinano se un membro viene restituito nella matrice di oggetti MemberInfo
.The search criteria that determines whether a member is returned in the array of MemberInfo
objects.
I campi di FieldAttributes
, MethodAttributes
e MethodImplAttributes
possono essere usati assieme al delegato FilterAttribute
fornito da questa classe.The fields of FieldAttributes
, MethodAttributes
, and MethodImplAttributes
can be used in conjunction with the FilterAttribute
delegate supplied by this class.
Restituisce
Matrice filtrata di oggetti MemberInfo del tipo di membro specificato.A filtered array of MemberInfo objects of the specified member type.
-oppure--or-
Matrice vuota se l'oggetto Type corrente non ha membri di tipo memberType
corrispondenti ai criteri del filtro.An empty array if the current Type does not have members of type memberType
that match the filter criteria.
Implementazioni
Eccezioni
filter
è null
.filter
is null
.
Esempio
Nell'esempio seguente vengono trovati tutti i membri di una classe che corrispondono ai criteri di ricerca specificati, quindi vengono visualizzati i membri corrispondenti.The following example finds all the members in a class that match the specified search criteria, and then displays the matched members.
using namespace System;
using namespace System::Reflection;
ref class MyFindMembersClass
{
public:
static void Test()
{
Object^ objTest = gcnew Object;
Type^ objType = objTest->GetType();
array<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), gcnew MemberFilter( DelegateToSearchCriteria ), "ReferenceEquals" );
for ( int index = 0; index < arrayMemberInfo->Length; index++ )
Console::WriteLine( "Result of FindMembers -\t {0}", String::Concat( arrayMemberInfo[ index ], "\n" ) );
}
catch ( Exception^ e )
{
Console::WriteLine( "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();
}
/* The example produces the following output:
Result of FindMembers - Boolean ReferenceEquals(System.Object, System.Object)
*/
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;
}
}
/* The example produces the following output:
Result of FindMembers - Boolean ReferenceEquals(System.Object, System.Object)
*/
Imports System.Reflection
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
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
' The example produces the following output:
'
' Result of FindMembers - Boolean ReferenceEquals(System.Object, System.Object)
Commenti
Questo metodo può essere sottoposto a override da una classe derivata.This method can be overridden by a derived class.
I membri includono proprietà, metodi, campi, eventi e così via.Members include properties, methods, fields, events, and so on.
Affinché il FindMembers
metodo recuperi correttamente le informazioni sui membri, l' bindingAttr
argomento deve includere almeno uno di BindingFlags.Instance e BindingFlags.Static , insieme ad almeno uno dei metodi BindingFlags.NonPublic e BindingFlags.Public .For the FindMembers
method to successfully retrieve member information, the bindingAttr
argument must include at least one of BindingFlags.Instance and BindingFlags.Static, along with at least one of BindingFlags.NonPublic and BindingFlags.Public.
I BindingFlags flag di filtro seguenti possono essere usati per definire i membri da includere nella ricerca:The following BindingFlags filter flags can be used to define which members to include in the search:
BindingFlags.Instance
Consente di specificare di includere i membri di istanza nella ricerca.SpecifyBindingFlags.Instance
to include instance members in the search.Specificare
BindingFlags.Static
per includere i membri statici nella ricerca.SpecifyBindingFlags.Static
to include static members in the search.BindingFlags.Public
Consente di specificare di includere i membri pubblici nella ricerca.SpecifyBindingFlags.Public
to include public members in the search.Specificare
BindingFlags.NonPublic
per includere i membri non pubblici (ovvero i membri privati, interni e protetti) nella ricerca.SpecifyBindingFlags.NonPublic
to include non-public members (that is, private, internal, and protected members) in the search.
I BindingFlags flag di modifica seguenti possono essere utilizzati per modificare il funzionamento della ricerca:The following BindingFlags modifier flags can be used to change how the search works:
BindingFlags.DeclaredOnly
per cercare solo i membri dichiarati in Type , non i membri che sono stati semplicemente ereditati.BindingFlags.DeclaredOnly
to search only the members declared on the Type, not members that were simply inherited.
Per altre informazioni, vedere System.Reflection.BindingFlags.See System.Reflection.BindingFlags for more information.
Per ottenere l'inizializzatore di classe (costruttore statico) utilizzando questo metodo, è necessario specificare BindingFlags.Static | BindingFlags.NonPublic ( BindingFlags.Static Or
BindingFlags.NonPublic in Visual Basic).To get the class initializer (static constructor) using this method, you must specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOr
BindingFlags.NonPublic in Visual Basic). È anche possibile ottenere l'inizializzatore di classe usando la TypeInitializer Proprietà.You can also get the class initializer using the TypeInitializer property.
Se l'oggetto corrente Type rappresenta un parametro di tipo di un tipo o di un metodo generico, FindMembers elabora tutti i membri dichiarati dal vincolo di classe e i vincoli di interfaccia del parametro di tipo.If the current Type represents a type parameter of a generic type or generic method, FindMembers processes any members declared by the class constraint and the interface constraints of the type parameter.
L' filter
argomento può essere un delegato personalizzato di tipo MemberFilter oppure può essere uno dei delegati predefiniti seguenti:The filter
argument can be a custom delegate of type MemberFilter, or it can be one of the following predefined delegates:
Type.FilterAttribute, che usa una FieldAttributes MethodAttributes maschera di maschera, o MethodImplAttributes come
filterCriteria
valore.Type.FilterAttribute, which uses aFieldAttributes, MethodAttributes, or MethodImplAttributes bitmask as thefilterCriteria
value.Type.FilterName, che esegue un confronto con distinzione tra maiuscole e minuscole di ogni nome di membro con la stringa passata a
filterCriteria
.Type.FilterName, which performs a case-sensitive comparison of each member name with the string passed tofilterCriteria
.Type.FilterNameIgnoreCase, che esegue un confronto senza distinzione tra maiuscole e minuscole di ogni nome di membro con la stringa passata a
filterCriteria
.Type.FilterNameIgnoreCase, which performs a case-insensitive comparison of each member name with the string passed tofilterCriteria
.