Type.FindMembers(MemberTypes, BindingFlags, MemberFilter, Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回指定成员类型的 MemberInfo 对象的筛选数组。
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()
参数
- memberType
- MemberTypes
枚举值的按位组合,它指示要搜索的成员的类型。
- filter
- MemberFilter
执行比较的委托,如果当前被检查的成员匹配 true
,则返回 filterCriteria
;否则返回 false
。
- filterCriteria
- Object
确定成员是否在 MemberInfo
对象数组中返回的搜索判据。
FieldAttributes
、MethodAttributes
和 MethodImplAttributes
的字段可以和该类提供的 FilterAttribute
委托一起使用。
返回
指定成员类型的 MemberInfo 对象的筛选数组。
或
如果当前 Type 没有与筛选条件相匹配的 memberType
类型的成员,则为空数组。
实现
例外
filter
上声明的默认值为 null
。
示例
以下示例查找类中与指定搜索条件匹配的所有成员,然后显示匹配的成员。
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)
*/
open System.Reflection
let delegateToSearchCriteria (objMemberInfo: MemberInfo) (objSearch: obj) =
// Compare the name of the member function with the filter criteria.
string objMemberInfo.Name = string objSearch
let objTest = obj ()
let objType = objTest.GetType ()
try
//Find all static or public methods in the Object class that match the specified name.
let arrayMemberInfo =
objType.FindMembers(MemberTypes.Method, BindingFlags.Public ||| BindingFlags.Static ||| BindingFlags.Instance, MemberFilter delegateToSearchCriteria, "ReferenceEquals")
for info in arrayMemberInfo do
printfn $"Result of FindMembers -\t{info}\n"
with e ->
printfn $"Exception : {e}"
(* 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)
注解
派生类可以重写此方法。
成员包括属性、方法、字段、事件等。
FindMembers
若要使 方法成功检索成员信息,bindingAttr
参数必须至少包含 和 BindingFlags.Static的BindingFlags.Instance一个 ,以及 至少一个 BindingFlags.NonPublic 和 BindingFlags.Public。
以下 BindingFlags 筛选器标志可用于定义要在搜索中包括的成员:
指定
BindingFlags.Instance
以在搜索中包含实例成员。指定
BindingFlags.Static
以在搜索中包含静态成员。指定
BindingFlags.Public
以在搜索中包含公共成员。指定
BindingFlags.NonPublic
以包括非公共成员 (,即在搜索中) 私有成员、内部成员和受保护成员。
以下 BindingFlags 修饰符标志可用于更改搜索的工作方式:
-
BindingFlags.DeclaredOnly
,仅搜索 上 Type声明的成员,而不搜索仅继承的成员。
有关更多信息,请参见System.Reflection.BindingFlags。
若要使用此方法获取类初始值设定项 (静态构造函数) ,必须在 Visual Basic) BindingFlags.StaticOr
BindingFlags.NonPublic 中指定BindingFlags.Static | BindingFlags.NonPublic (。 还可以使用 TypeInitializer 属性获取类初始值设定项。
如果当前 Type 表示泛型类型或泛型方法的类型参数, FindMembers 则处理类约束声明的任何成员以及类型参数的接口约束。
参数 filter
可以是 类型的 MemberFilter自定义委托,也可以是以下预定义委托之一:
Type.FilterAttribute,它使用FieldAttributes 、 MethodAttributes或 MethodImplAttributes 位掩码作为
filterCriteria
值。Type.FilterName,用于对每个成员名称与传递给
filterCriteria
的字符串执行区分大小写的比较。Type.FilterNameIgnoreCase,用于对每个成员名称与传递给
filterCriteria
的字符串执行不区分大小写的比较。