Module.FindTypes(TypeFilter, Object) Module.FindTypes(TypeFilter, Object) Module.FindTypes(TypeFilter, Object) Module.FindTypes(TypeFilter, Object) Method

定義

傳回指定篩選器和篩選準則所接受的類別陣列。Returns an array of classes accepted by the given filter and filter criteria.

public:
 virtual cli::array <Type ^> ^ FindTypes(System::Reflection::TypeFilter ^ filter, System::Object ^ filterCriteria);
public virtual Type[] FindTypes (System.Reflection.TypeFilter filter, object filterCriteria);
abstract member FindTypes : System.Reflection.TypeFilter * obj -> Type[]
override this.FindTypes : System.Reflection.TypeFilter * obj -> Type[]
Public Overridable Function FindTypes (filter As TypeFilter, filterCriteria As Object) As Type()

參數

filter
TypeFilter TypeFilter TypeFilter TypeFilter

用來篩選類別的委派。The delegate used to filter the classes.

filterCriteria
Object Object Object Object

用來篩選類別的物件。An Object used to filter the classes.

傳回

Type[]

類型 Type 的陣列,其中包含篩選器所接受的類別。An array of type Type containing classes that were accepted by the filter.

例外狀況

無法載入模組中的一或多個類別。One or more classes in a module could not be loaded.

範例

下列範例示範FindTypes方法。The following example demonstrates the FindTypes method.

using namespace System;
using namespace System::Reflection;
using namespace System::Collections;
public ref class MySecondClass{};


// This class does not fit the filter criterion My*.
public ref class YourClass{};

int main()
{
   array<Module^>^moduleArray;
   moduleArray = Assembly::GetExecutingAssembly()->GetModules( false );
   
   // In a simple project with only one module, the module at index
   // 0 will be the module containing these classes.
   Module^ myModule = moduleArray[ 0 ];
   array<Type^>^tArray;
   tArray = myModule->FindTypes( Module::FilterTypeName, "My*" );
   IEnumerator^ myEnum = tArray->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Type^ t = safe_cast<Type^>(myEnum->Current);
      Console::WriteLine( "Found a module beginning with My*: {0}.", t->Name );
   }
}

using System;
using System.Reflection;

namespace ReflectionModule_Examples
{
    class MyMainClass
    {
        static void Main()
        {
            Module[] moduleArray;
            
            moduleArray = typeof(MyMainClass).Assembly.GetModules(false);
            
            // In a simple project with only one module, the module at index
            // 0 will be the module containing these classes.
            Module myModule = moduleArray[0];

            Type[] tArray;

            tArray = myModule.FindTypes(Module.FilterTypeName, "My*");
            
            foreach(Type t in tArray)
            {
                Console.WriteLine("Found a module beginning with My*: {0}.", t.Name);
            }
        }
    }

    class MySecondClass
    {
    }

    // This class does not fit the filter criteria My*.
    class YourClass
    {
    }
}
Imports System.Reflection

Namespace ReflectionModule_Examples
    Class MyMainClass
        Shared Sub Main()
            Dim moduleArray() As [Module]

            moduleArray = GetType(MyMainClass).Assembly.GetModules(False)

            ' In a simple project with only one module, the module at index
            ' 0 will be the module containing these classes.
            Dim myModule As [Module] = moduleArray(0)

            Dim tArray() As Type

            tArray = myModule.FindTypes([Module].FilterTypeName, "My*")

            Dim t As Type
            For Each t In tArray
                Console.WriteLine("Found a module beginning with My*: {0}", t.Name)
            Next t
        End Sub 'Main
    End Class 'MyMainClass

    Class MySecondClass
    End Class 'MySecondClass

    ' This class does not fit the filter criteria My*.
    Class YourClass
    End Class 'YourClass
End Namespace 'ReflectionModule_Examples

備註

ReflectionTypeLoadException 是特殊類別載入例外狀況。ReflectionTypeLoadException is a special class load exception. ReflectionTypeLoadException.Types屬性包含的模組中所定義,且已載入類別之陣列。The ReflectionTypeLoadException.Types property contains the array of classes that were defined in the module and were loaded. 這個陣列可包含部分 null 值。This array may contain some null values. ReflectionTypeLoadException.LoaderExceptions屬性為陣列,代表類別載入器擲回的例外狀況的例外狀況。The ReflectionTypeLoadException.LoaderExceptions property is an array of exceptions that represent the exceptions that were thrown by the class loader. 類別陣列漏洞產品有例外狀況。The holes in the class array line up with the exceptions.

所指定的委派filter會呼叫每個類別,在模組中,傳遞Type物件,表示類別,以及指定filterCriteriaThe delegate given by filter is called for each class in the module, passing along the Type object representing the class as well as the given filterCriteria. 如果filter傳回特定類別,該類別將會包含在傳回的陣列。If filter returns a particular class, that class will be included in the returned array. 如果filter會傳回null,會傳回所有類別和filterCriteria會被忽略。If filter returns null, all classes are returned and filterCriteria is ignored.

FindTypes 不能用於查詢參數化的型別,例如陣列。FindTypes cannot be used to look up parameterized types such as arrays.

適用於

另請參閱