Module.GetType Module.GetType Module.GetType Module.GetType Method

定义

返回指定的类型。Returns the specified type.

重载

GetType(String) GetType(String) GetType(String) GetType(String)

返回指定的类型,执行区分大小写的搜索。Returns the specified type, performing a case-sensitive search.

GetType(String, Boolean) GetType(String, Boolean) GetType(String, Boolean) GetType(String, Boolean)

返回指定的类型,通过指定的区分大小写搜索模块。Returns the specified type, searching the module with the specified case sensitivity.

GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean)

返回指定的类型,指定是否对该模块进行区分大小写的搜索;如果找不到该类型,则指定是否引发异常。Returns the specified type, specifying whether to make a case-sensitive search of the module and whether to throw an exception if the type cannot be found.

GetType(String) GetType(String) GetType(String) GetType(String)

返回指定的类型,执行区分大小写的搜索。Returns the specified type, performing a case-sensitive search.

public:
 virtual Type ^ GetType(System::String ^ className);
[System.Runtime.InteropServices.ComVisible(true)]
public virtual Type GetType (string className);
override this.GetType : string -> Type
Public Overridable Function GetType (className As String) As Type

参数

className
String String String String

要查找的类型的名称。The name of the type to locate. 名称必须用命名空间加以完全限定。The name must be fully qualified with the namespace.

返回

表示给定类型的 Type 对象(如果类型位于此模块中);否则为 nullA Type object representing the given type, if the type is in this module; otherwise, null.

异常

调用类初始值设定项,并且引发异常。The class initializers are invoked and an exception is thrown.

className 是一个长度为零的字符串。className is a zero-length string.

className 所需的从属程序集无法找到。className requires a dependent assembly that could not be found.

className 所需的从属程序集已找到,但无法加载。className requires a dependent assembly that was found but could not be loaded.

-or- 当前程序集被加载到仅反射上下文中,并且 className 所需的从属程序集未预先加载。The current assembly was loaded into the reflection-only context, and className requires a dependent assembly that was not preloaded.

className 需要一个从属程序集,但该文件不是有效的程序集。className requires a dependent assembly, but the file is not a valid assembly.

-or- className 需要一个从属程序集,该程序集已针对比当前加载的版本更高的运行时版本进行了编译。className requires a dependent assembly which was compiled for a version of the runtime later than the currently loaded version.

示例

下面的示例显示指定模块中的类型的名称。The following example displays the name of a type in the specified module.

using namespace System;
using namespace System::Reflection;

namespace ReflectionModule_Examples
{
   public ref class MyMainClass{};

}

int main()
{
   array<Module^>^moduleArray;
   moduleArray = ReflectionModule_Examples::MyMainClass::typeid->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^ myType;
   myType = myModule->GetType( "ReflectionModule_Examples.MyMainClass" );
   Console::WriteLine( "Got type: {0}", myType );
}

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 myType;

            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass");
            Console.WriteLine("Got type: {0}", myType.ToString());
        }
    }
}
Imports System.Reflection

'This code assumes that the root namespace is set to empty("").
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 myType As Type

            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass")
            Console.WriteLine("Got type: {0}", myType.ToString())
        End Sub
    End Class
End Namespace 'ReflectionModule_Examples

注解

备注

如果已将该类型转发给另一个程序集,则此方法仍将返回该程序集。If the type has been forwarded to another assembly, it is still returned by this method. 有关类型转发的信息,请参阅公共语言运行时中的类型转发For information on type forwarding, see Type Forwarding in the Common Language Runtime.

可使用 Module.GetType 从特定模块检索类型。A type can be retrieved from a specific module using Module.GetType. 对包含清单的模块调用 Module.GetType 不会搜索整个程序集。Calling Module.GetType on the module that contains the manifest will not search the entire assembly. 若要从程序集检索类型(无论它位于哪个模块),必须调用 Assembly.GetTypeTo retrieve a type from an assembly, regardless of which module it is in, you must call Assembly.GetType.

GetType(String, Boolean) GetType(String, Boolean) GetType(String, Boolean) GetType(String, Boolean)

返回指定的类型,通过指定的区分大小写搜索模块。Returns the specified type, searching the module with the specified case sensitivity.

public:
 virtual Type ^ GetType(System::String ^ className, bool ignoreCase);
[System.Runtime.InteropServices.ComVisible(true)]
public virtual Type GetType (string className, bool ignoreCase);
override this.GetType : string * bool -> Type
Public Overridable Function GetType (className As String, ignoreCase As Boolean) As Type

参数

className
String String String String

要查找的类型的名称。The name of the type to locate. 名称必须用命名空间加以完全限定。The name must be fully qualified with the namespace.

ignoreCase
Boolean Boolean Boolean Boolean

对于不区分大小写的搜索,为 true;否则为 falsetrue for case-insensitive search; otherwise, false.

返回

表示给定类型的 Type 对象(如果类型位于此模块中);否则为 nullA Type object representing the given type, if the type is in this module; otherwise, null.

异常

调用类初始值设定项,并且引发异常。The class initializers are invoked and an exception is thrown.

className 是一个长度为零的字符串。className is a zero-length string.

className 所需的从属程序集无法找到。className requires a dependent assembly that could not be found.

className 所需的从属程序集已找到,但无法加载。className requires a dependent assembly that was found but could not be loaded.

- 或 --or- 当前程序集被加载到仅反射上下文中,并且 className 所需的从属程序集未预先加载。The current assembly was loaded into the reflection-only context, and className requires a dependent assembly that was not preloaded.

className 需要一个从属程序集,但该文件不是有效的程序集。className requires a dependent assembly, but the file is not a valid assembly.

-or- className 需要一个从属程序集,该程序集已针对比当前加载的版本更高的运行时版本进行了编译。className requires a dependent assembly which was compiled for a version of the runtime later than the currently loaded version.

示例

下面的示例显示指定模块中的类型名称,并为 @no__t 的参数指定 false,以便不会忽略此情况。The following example displays the name of a type in the specified module, specifying false for the ignoreCase parameter so that case will not be ignored.

using namespace System;
using namespace System::Reflection;

namespace ReflectionModule_Examples
{
   public ref class MyMainClass{};

}

int main()
{
   array<Module^>^moduleArray;
   moduleArray = ReflectionModule_Examples::MyMainClass::typeid->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^ myType;
   myType = myModule->GetType( "ReflectionModule_Examples.MyMainClass", false );
   Console::WriteLine( "Got type: {0}", myType );
}

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 myType;
            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass", false);
            Console.WriteLine("Got type: {0}", myType.ToString());
        }
    }
}
Imports System.Reflection

'This code assumes that the root namespace is set to empty("").
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 myType As Type
            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass", False)
            Console.WriteLine("Got type: {0}", myType.ToString())
        End Sub
    End Class
End Namespace 'ReflectionModule_Examples

注解

备注

如果已将该类型转发给另一个程序集,则此方法仍将返回该程序集。If the type has been forwarded to another assembly, it is still returned by this method. 有关类型转发的信息,请参阅公共语言运行时中的类型转发For information on type forwarding, see Type Forwarding in the Common Language Runtime.

可使用 Module.GetType 从特定模块检索类型。A type can be retrieved from a specific module using Module.GetType. 对包含清单的模块调用 Module.GetType 不会搜索整个程序集。Calling Module.GetType on the module that contains the manifest will not search the entire assembly. 若要从程序集检索类型(无论它位于哪个模块),必须调用 Assembly.GetTypeTo retrieve a type from an assembly, regardless of which module it is in, you must call Assembly.GetType.

GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean)

返回指定的类型,指定是否对该模块进行区分大小写的搜索;如果找不到该类型,则指定是否引发异常。Returns the specified type, specifying whether to make a case-sensitive search of the module and whether to throw an exception if the type cannot be found.

public:
 virtual Type ^ GetType(System::String ^ className, bool throwOnError, bool ignoreCase);
[System.Runtime.InteropServices.ComVisible(true)]
public virtual Type GetType (string className, bool throwOnError, bool ignoreCase);
override this.GetType : string * bool * bool -> Type
Public Overridable Function GetType (className As String, throwOnError As Boolean, ignoreCase As Boolean) As Type

参数

className
String String String String

要查找的类型的名称。The name of the type to locate. 名称必须用命名空间加以完全限定。The name must be fully qualified with the namespace.

throwOnError
Boolean Boolean Boolean Boolean

如果为 true,则在找不到该类型时引发异常;如果为 false,则返回 nulltrue to throw an exception if the type cannot be found; false to return null.

ignoreCase
Boolean Boolean Boolean Boolean

对于不区分大小写的搜索,为 true;否则为 falsetrue for case-insensitive search; otherwise, false.

返回

如果已在此模块中声明指定类型,则为一个表示指定类型的 Type 对象;否则为 nullA Type object representing the specified type, if the type is declared in this module; otherwise, null.

异常

调用类初始值设定项,并且引发异常。The class initializers are invoked and an exception is thrown.

className 是一个长度为零的字符串。className is a zero-length string.

throwOnErrortrue,且找不到此类型。throwOnError is true, and the type cannot be found.

className 所需的从属程序集无法找到。className requires a dependent assembly that could not be found.

className 所需的从属程序集已找到,但无法加载。className requires a dependent assembly that was found but could not be loaded.

- 或 --or- 当前程序集被加载到仅反射上下文中,并且 className 所需的从属程序集未预先加载。The current assembly was loaded into the reflection-only context, and className requires a dependent assembly that was not preloaded.

className 需要一个从属程序集,但该文件不是有效的程序集。className requires a dependent assembly, but the file is not a valid assembly.

- 或 --or- className 需要一个从属程序集,该程序集已针对比当前加载的版本更高的运行时版本进行了编译。className requires a dependent assembly which was compiled for a version of the runtime later than the currently loaded version.

示例

下面的示例显示指定模块中的类型的名称。The following example displays the name of a type in the specified module. @No__t-0 和 @no__t 参数均指定为 falseThe throwOnError and ignoreCase parameters are specified as false.

using namespace System;
using namespace System::Reflection;

namespace ReflectionModule_Examples
{
   public ref class MyMainClass{};

}

int main()
{
   array<Module^>^moduleArray;
   moduleArray = ReflectionModule_Examples::MyMainClass::typeid->Assembly->GetModules( false );
   
   //In a simple project with only one module, the module at index
   // 0 will be the module containing this class.
   Module^ myModule = moduleArray[ 0 ];
   Type^ myType;
   myType = myModule->GetType( "ReflectionModule_Examples.MyMainClass", false, false );
   Console::WriteLine( "Got type: {0}", myType );
}

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 this class.
            Module myModule = moduleArray[0];

            Type myType;
            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass", false, false);
            Console.WriteLine("Got type: {0}", myType.ToString());
        }
    }
}
Imports System.Reflection

'This code assumes that the root namespace is set to empty("").
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 this class.
            Dim myModule As [Module] = moduleArray(0)

            Dim myType As Type
            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass", False, False)
            Console.WriteLine("Got type: {0}", myType.ToString())
        End Sub
    End Class
End Namespace 'ReflectionModule_Examples

注解

@No__t-0 参数仅影响找不到该类型时所发生的情况。The throwOnError parameter affects only what happens when the type is not found. 它不会影响可能引发的任何其他异常。It does not affect any other exceptions that might be thrown. 具体而言,如果找到类型但无法加载,则即使 @no__t 为 false,也可能引发 TypeLoadExceptionIn particular, if the type is found but cannot be loaded, TypeLoadException can be thrown even if throwOnError is false.

备注

如果已将该类型转发给另一个程序集,则此方法仍将返回该程序集。If the type has been forwarded to another assembly, it is still returned by this method. 有关类型转发的信息,请参阅公共语言运行时中的类型转发For information on type forwarding, see Type Forwarding in the Common Language Runtime.

可使用 Module.GetType 从特定模块检索类型。A type can be retrieved from a specific module using Module.GetType. 对包含清单的模块调用 Module.GetType 不会搜索整个程序集。Calling Module.GetType on the module that contains the manifest will not search the entire assembly. 若要从程序集检索类型(无论它位于哪个模块),必须调用 Assembly.GetTypeTo retrieve a type from an assembly, regardless of which module it is in, you must call Assembly.GetType.

适用于