Type.FindInterfaces 方法
返回表示接口(由当前 Type 所实现或继承)的筛选列表的 Type 对象数组。
**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Overridable Function FindInterfaces ( _
filter As TypeFilter, _
filterCriteria As Object _
) As Type()
用法
Dim instance As Type
Dim filter As TypeFilter
Dim filterCriteria As Object
Dim returnValue As Type()
returnValue = instance.FindInterfaces(filter, filterCriteria)
public virtual Type[] FindInterfaces (
TypeFilter filter,
Object filterCriteria
)
public:
virtual array<Type^>^ FindInterfaces (
TypeFilter^ filter,
Object^ filterCriteria
)
public Type[] FindInterfaces (
TypeFilter filter,
Object filterCriteria
)
public function FindInterfaces (
filter : TypeFilter,
filterCriteria : Object
) : Type[]
参数
- filter
对照 filterCriteria 比较接口的 TypeFilter 委托。
- filterCriteria
确定接口是否应包括在返回数组中的搜索判据。
返回值
一个表示当前 Type 实现或继承的接口的筛选列表的 Type 对象数组,或者类型 Type 的空数组(如果当前 Type 没有实现或继承匹配筛选器的接口)。
异常
异常类型 | 条件 |
---|---|
filter 为 空引用(在 Visual Basic 中为 Nothing)。 |
|
调用静态初始值设定项并引发异常。 |
备注
此方法可由派生类重写。
System.Reflection.Module 类提供的 Module.FilterTypeName 和 Module.FilterTypeNameIgnoreCase 委托也可能被使用,以替代 System.Reflection.TypeFilter 委托。
无论是由基类声明还是由该类自己声明,该类实现的所有接口在搜索期间都会被考虑。
此方法搜索基类层次结构,返回每个类所实现的每个匹配接口,以及那些接口中的每一个所实现的所有匹配接口(即返回匹配接口的可传递结束)。不返回重复的接口。
如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则 FindInterfaces 搜索类型参数的约束中声明的所有接口,以及通过约束中声明的接口继承的所有接口。如果当前 Type 表示泛型类型的类型实参,则 FindInterfaces 搜索该类型实现的所有接口,而不管它们是否匹配那些约束。
提示
即使在非泛型类型上,FindInterfaces 也可能会返回泛型接口。例如,某个非泛型类型可能实现了 IEnumerable<int>
(在 Visual Basic 中为 IEnumerable(Of Integer)
)。
示例
下面的示例查找由指定的类型实现或继承的指定接口,然后显示这些接口名称。
Imports System
Imports System.Xml
Imports System.Reflection
Imports Microsoft.VisualBasic
Public Class MyFindInterfacesSample
Public Shared Sub Main()
Try
Dim myXMLDoc As New XmlDocument()
myXMLDoc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" _
& "<title>Pride And Prejudice</title>" & "</book>")
Dim myType As Type = myXMLDoc.GetType()
' Specify the TypeFilter delegate that compares the
' interfaces against filter criteria.
Dim myFilter As New TypeFilter(AddressOf MyInterfaceFilter)
Dim myInterfaceList() As String = _
{"System.Collections.IEnumerable", _
"System.Collections.ICollection"}
Dim index As Integer
For index = 0 To myInterfaceList.Length - 1
Dim myInterfaces As Type() = _
myType.FindInterfaces(myFilter, myInterfaceList(index))
If myInterfaces.Length > 0 Then
Console.WriteLine(ControlChars.Cr & _
"{0} implements the interface {1}.", _
myType, myInterfaceList(index))
Dim j As Integer
For j = 0 To myInterfaces.Length - 1
Console.WriteLine("Interfaces supported: {0}", _
myInterfaces(j).ToString())
Next j
Else
Console.WriteLine(ControlChars.Cr & _
"{0} does not implement the interface {1}.", _
myType, myInterfaceList(index))
End If
Next index
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: " & e.Message)
Catch e As TargetInvocationException
Console.WriteLine("TargetInvocationException: " & e.Message)
Catch e As Exception
Console.WriteLine("Exception: " & e.Message)
End Try
End Sub 'Main
Public Shared Function MyInterfaceFilter(ByVal typeObj As Type, _
ByVal criteriaObj As [Object]) As Boolean
If typeObj.ToString() = criteriaObj.ToString() Then
Return True
Else
Return False
End If
End Function 'MyInterfaceFilter
End Class 'MyFindInterfacesSample
using System;
using System.Xml;
using System.Reflection;
public class MyFindInterfacesSample
{
public static void Main()
{
try
{
XmlDocument myXMLDoc = new XmlDocument();
myXMLDoc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" + "</book>");
Type myType = myXMLDoc.GetType();
// Specify the TypeFilter delegate that compares the
// interfaces against filter criteria.
TypeFilter myFilter = new TypeFilter(MyInterfaceFilter);
String[] myInterfaceList = new String[2]
{"System.Collections.IEnumerable",
"System.Collections.ICollection"};
for(int index=0; index < myInterfaceList.Length; index++)
{
Type[] myInterfaces = myType.FindInterfaces(myFilter,
myInterfaceList[index]);
if (myInterfaces.Length > 0)
{
Console.WriteLine("\n{0} implements the interface {1}.",
myType, myInterfaceList[index]);
for(int j =0;j < myInterfaces.Length;j++)
Console.WriteLine("Interfaces supported: {0}.",
myInterfaces[j].ToString());
}
else
Console.WriteLine(
"\n{0} does not implement the interface {1}.",
myType,myInterfaceList[index]);
}
}
catch(ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: " + e.Message);
}
catch(TargetInvocationException e)
{
Console.WriteLine("TargetInvocationException: " + e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
public static bool MyInterfaceFilter(Type typeObj,Object criteriaObj)
{
if(typeObj.ToString() == criteriaObj.ToString())
return true;
else
return false;
}
}
#using <system.xml.dll>
using namespace System;
using namespace System::Xml;
using namespace System::Reflection;
public ref class MyFindInterfacesSample
{
public:
static bool MyInterfaceFilter(Type^ typeObj, Object^ criteriaObj)
{
if(typeObj->ToString()->Equals(criteriaObj->ToString()))
return true;
else
return false;
}
};
int main()
{
try
{
XmlDocument^ myXMLDoc = gcnew XmlDocument;
myXMLDoc->LoadXml("<book genre='novel' ISBN='1-861001-57-5'>"
+ "<title>Pride And Prejudice</title> </book>");
Type^ myType = myXMLDoc->GetType();
// Specify the TypeFilter delegate that compares the interfaces
// against filter criteria.
TypeFilter^ myFilter = gcnew TypeFilter(
MyFindInterfacesSample::MyInterfaceFilter);
array<String^>^myInterfaceList = {"System.Collections.IEnumerable",
"System.Collections.ICollection"};
for(int index = 0; index < myInterfaceList->Length; index++)
{
array<Type^>^myInterfaces = myType->FindInterfaces(
myFilter, myInterfaceList[index]);
if(myInterfaces->Length > 0)
{
Console::WriteLine("\n{0} implements the interface {1}.",
myType, myInterfaceList[index]);
for(int j = 0; j < myInterfaces->Length; j++)
Console::WriteLine("Interfaces supported: {0}.",
myInterfaces[j]);
}
else
Console::WriteLine(
"\n{0} does not implement the interface {1}.",
myType, myInterfaceList[index]);
}
}
catch(ArgumentNullException^ e)
{
Console::WriteLine("ArgumentNullException: {0}", e->Message);
}
catch(TargetInvocationException^ e)
{
Console::WriteLine("TargetInvocationException: {0}", e->Message);
}
catch(Exception^ e)
{
Console::WriteLine("Exception: {0}", e->Message);
}
}
import System.*;
import System.Xml.*;
import System.Reflection.*;
public class MyFindInterfacesSample
{
public static void main(String[] args)
{
try {
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>"
+ "<title>Pride And Prejudice</title>" + "</book>");
Type myType = myXmlDoc.GetType();
// Specify the TypeFilter delegate that compares the interfaces
// against filter criteria.
TypeFilter myFilter = new TypeFilter(MyInterfaceFilter);
String myInterfaceList[] = new String[] {
"System.Collections.IEnumerable",
"System.Collections.ICollection" };
for (int index = 0; index < myInterfaceList.length; index++) {
Type myInterfaces[] = myType.FindInterfaces(myFilter,
myInterfaceList.get_Item(index));
if (myInterfaces.length > 0) {
Console.WriteLine("\n{0} implements the interface {1}.",
myType, myInterfaceList.get_Item(index));
for (int j = 0; j < myInterfaces.length; j++) {
Console.WriteLine("Interfaces supported: {0}.",
myInterfaces.get_Item(j).ToString());
}
}
else {
Console.WriteLine("\n{0} does not implement the"
+ " interface {1}.", myType,
myInterfaceList.get_Item(index));
}
}
}
catch (ArgumentNullException e) {
Console.WriteLine("ArgumentNullException: " + e.get_Message());
}
catch (TargetInvocationException e) {
Console.WriteLine("TargetInvocationException: " + e.get_Message());
}
catch (System.Exception e) {
Console.WriteLine("Exception: " + e.get_Message());
}
} //main
public static boolean MyInterfaceFilter(Type typeObj, Object criteriaObj)
{
if (typeObj.ToString().Equals(criteriaObj.ToString())) {
return true;
}
else {
return false;
}
} //MyInterfaceFilter
} //MyFindInterfacesSample
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
请参见
参考
Type 类
Type 成员
System 命名空间
Module
TypeFilter
GetInterface
GetInterfaces