Type.FindMembers(MemberTypes, BindingFlags, MemberFilter, Object) Método
Definición
Devuelve una matriz filtrada de objetos MemberInfo del tipo del miembro especificado.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()
Parámetros
- memberType
- MemberTypes
Combinación bit a bit de los valores de enumeración que indica el tipo de miembro por el que se va a buscar.A bitwise combination of the enumeration values that indicates the type of member to search for.
- bindingAttr
- BindingFlags
Combinación bit a bit de los valores de enumeración que especifican cómo se realiza la búsqueda.A bitwise combination of the enumeration values that specify how the search is conducted.
o bien-or-
Default para devolver null
.Default to return null
.
- filter
- MemberFilter
Delegado que realiza las comparaciones y que devuelve true
si el miembro inspeccionado coincide con filterCriteria
y false
en caso contrario.The delegate that does the comparisons, returning true
if the member currently being inspected matches the filterCriteria
and false
otherwise.
- filterCriteria
- Object
Criterios de búsqueda que determinan si se devuelve un miembro en la matriz de objetos MemberInfo
.The search criteria that determines whether a member is returned in the array of MemberInfo
objects.
Los campos de FieldAttributes
, MethodAttributes
y MethodImplAttributes
se pueden usar junto con el delegado FilterAttribute
que esta clase proporciona.The fields of FieldAttributes
, MethodAttributes
, and MethodImplAttributes
can be used in conjunction with the FilterAttribute
delegate supplied by this class.
Devoluciones
Matriz filtrada de objetos MemberInfo del tipo del miembro especificado.A filtered array of MemberInfo objects of the specified member type.
o bien-or-
Matriz vacía si el objeto Type actual no tiene miembros del tipo memberType
que coincidan con los criterios de filtro.An empty array if the current Type does not have members of type memberType
that match the filter criteria.
Implementaciones
Excepciones
filter
es null
.filter
is null
.
Ejemplos
En el ejemplo siguiente se buscan todos los miembros de una clase que coincidan con los criterios de búsqueda especificados y, a continuación, se muestran los miembros coincidentes.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)
Comentarios
Este método se puede invalidar mediante una clase derivada.This method can be overridden by a derived class.
Los miembros incluyen propiedades, métodos, campos, eventos, etc.Members include properties, methods, fields, events, and so on.
Para FindMembers
que el método recupere correctamente la información de los miembros, el bindingAttr
argumento debe incluir al menos una de BindingFlags.Instance y BindingFlags.Static , junto con al menos uno de BindingFlags.NonPublic y 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.
BindingFlagsSe pueden usar las siguientes marcas de filtro para definir los miembros que se incluirán en la búsqueda:The following BindingFlags filter flags can be used to define which members to include in the search:
Especifique
BindingFlags.Instance
para incluir miembros de instancia en la búsqueda.SpecifyBindingFlags.Instance
to include instance members in the search.Especifique
BindingFlags.Static
la inclusión de miembros estáticos en la búsqueda.SpecifyBindingFlags.Static
to include static members in the search.Especifique
BindingFlags.Public
que se incluyan los miembros públicos en la búsqueda.SpecifyBindingFlags.Public
to include public members in the search.Especifique
BindingFlags.NonPublic
que se incluyan miembros no públicos (es decir, miembros privados, internos y protegidos) en la búsqueda.SpecifyBindingFlags.NonPublic
to include non-public members (that is, private, internal, and protected members) in the search.
BindingFlagsSe pueden usar las siguientes marcas de modificador para cambiar el funcionamiento de la búsqueda:The following BindingFlags modifier flags can be used to change how the search works:
BindingFlags.DeclaredOnly
para buscar solo los miembros declarados en Type , no los miembros que se han heredado simplemente.BindingFlags.DeclaredOnly
to search only the members declared on the Type, not members that were simply inherited.
Vea System.Reflection.BindingFlags para obtener más información.See System.Reflection.BindingFlags for more information.
Para obtener el inicializador de clase (constructor estático) con este método, debe especificar BindingFlags.Static | BindingFlags.NonPublic ( BindingFlags.Static Or
BindingFlags.NonPublic en 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). También puede obtener el inicializador de clase mediante la TypeInitializer propiedad.You can also get the class initializer using the TypeInitializer property.
Si el actual Type representa un parámetro de tipo de un tipo genérico o de un método genérico, FindMembers procesa los miembros declarados por la restricción de clase y las restricciones de interfaz del parámetro de 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.
El filter
argumento puede ser un delegado personalizado de tipo MemberFilter o puede ser uno de los siguientes delegados predefinidos:The filter
argument can be a custom delegate of type MemberFilter, or it can be one of the following predefined delegates:
Type.FilterAttribute, que usa una FieldAttributes MethodAttributes máscara de máscara, o MethodImplAttributes como
filterCriteria
valor.Type.FilterAttribute, which uses aFieldAttributes, MethodAttributes, or MethodImplAttributes bitmask as thefilterCriteria
value.Type.FilterName, que realiza una comparación que distingue entre mayúsculas y minúsculas de cada nombre de miembro con la cadena que se pasa a
filterCriteria
.Type.FilterName, which performs a case-sensitive comparison of each member name with the string passed tofilterCriteria
.Type.FilterNameIgnoreCase, que realiza una comparación sin distinción entre mayúsculas y minúsculas de cada nombre de miembro con la cadena que se pasa a
filterCriteria
.Type.FilterNameIgnoreCase, which performs a case-insensitive comparison of each member name with the string passed tofilterCriteria
.