Module.GetType 方法

定义

返回指定的类型。

重载

GetType(String)

返回指定的类型,执行区分大小写的搜索。

GetType(String, Boolean)

返回指定的类型,通过指定的区分大小写搜索模块。

GetType(String, Boolean, Boolean)

返回指定的类型,指定是否对该模块进行区分大小写的搜索;如果找不到该类型,则指定是否引发异常。

GetType(String)

返回指定的类型,执行区分大小写的搜索。

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

参数

className
String

要查找的类型的名称。 名称必须用命名空间加以完全限定。

返回

Type

表示给定类型的 Type 对象(如果类型位于此模块中);否则为 null

属性

例外

classNamenull

调用类初始值设定项,并且引发异常。

className 是一个长度为零的字符串。

className 所需的从属程序集无法找到。

className 所需的从属程序集已找到,但无法加载。

  • 或 - 当前程序集被加载到仅反射上下文中,并且 className 所需的从属程序集未预先加载。

className 需要一个从属程序集,但该文件不是有效的程序集。

  • 或 - className 需要一个从属程序集,该程序集已针对比当前加载的版本更高的运行时版本进行了编译。

示例

以下示例显示指定模块中类型的名称。

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

注解

备注

如果类型已转发到另一个程序集,则此方法仍返回该类型。 有关类型转发的信息,请参阅 公共语言运行时 中的类型转发

可以使用 从特定模块检索类型 Module.GetType 。 对 Module.GetType 包含清单的模块调用 不会搜索整个程序集。 若要从程序集中检索类型,无论它位于哪个模块中,都必须调用 Assembly.GetType

适用于

GetType(String, Boolean)

返回指定的类型,通过指定的区分大小写搜索模块。

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

参数

className
String

要查找的类型的名称。 名称必须用命名空间加以完全限定。

ignoreCase
Boolean

对于不区分大小写的搜索,为 true;否则为 false

返回

Type

表示给定类型的 Type 对象(如果类型位于此模块中);否则为 null

属性

例外

classNamenull

调用类初始值设定项,并且引发异常。

className 是一个长度为零的字符串。

className 所需的从属程序集无法找到。

className 所需的从属程序集已找到,但无法加载。

  • 或 - 当前程序集被加载到仅反射上下文中,并且 className 所需的从属程序集未预先加载。

className 需要一个从属程序集,但该文件不是有效的程序集。

  • 或 - className 需要一个从属程序集,该程序集已针对比当前加载的版本更高的运行时版本进行了编译。

示例

以下示例显示指定模块中类型的名称,为 参数指定 ,以便不会 false ignoreCase 忽略大小写。

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

注解

备注

如果类型已转发到另一个程序集,则此方法仍返回该类型。 有关类型转发的信息,请参阅 公共语言运行时 中的类型转发

可以使用 从特定模块检索类型 Module.GetType 。 对 Module.GetType 包含清单的模块调用 不会搜索整个程序集。 若要从程序集中检索类型,无论它位于哪个模块中,都必须调用 Assembly.GetType

适用于

GetType(String, Boolean, Boolean)

返回指定的类型,指定是否对该模块进行区分大小写的搜索;如果找不到该类型,则指定是否引发异常。

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

参数

className
String

要查找的类型的名称。 名称必须用命名空间加以完全限定。

throwOnError
Boolean

如果为 true,则在找不到该类型时引发异常;如果为 false,则返回 null

ignoreCase
Boolean

对于不区分大小写的搜索,为 true;否则为 false

返回

Type

如果已在此模块中声明指定类型,则为一个表示指定类型的 Type 对象;否则为 null

属性

例外

classNamenull

调用类初始值设定项,并且引发异常。

className 是一个长度为零的字符串。

throwOnErrortrue,且找不到此类型。

className 所需的从属程序集无法找到。

className 所需的从属程序集已找到,但无法加载。

  • 或 - 当前程序集被加载到仅反射上下文中,并且 className 所需的从属程序集未预先加载。

className 需要一个从属程序集,但该文件不是有效的程序集。

  • 或 - className 需要一个从属程序集,该程序集已针对比当前加载的版本更高的运行时版本进行了编译。

示例

以下示例显示指定模块中类型的名称。 和 throwOnError ignoreCase 参数指定为 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

注解

throwOnError参数仅影响找不到类型时发生的情况。 它不会影响可能引发的任何其他异常。 具体而言,如果找到类型但无法加载 ,则即使 TypeLoadException 为 ,也可以 throwOnError 引发 false

备注

如果类型已转发到另一个程序集,则此方法仍返回该类型。 有关类型转发的信息,请参阅 公共语言运行时 中的类型转发

可以使用 从特定模块检索类型 Module.GetType 。 对 Module.GetType 包含清单的模块调用 不会搜索整个程序集。 若要从程序集中检索类型,无论它位于哪个模块中,都必须调用 Assembly.GetType

适用于