Assembly.GetType 方法

定义

获取表示指定类型的 Type 对象。Gets the Type object that represents the specified type.

重载

GetType(String, Boolean, Boolean)

获取程序集实例中具有指定名称的 Type 对象,带有忽略大小写和在找不到该类型时引发异常的选项。Gets the Type object with the specified name in the assembly instance, with the options of ignoring the case, and of throwing an exception if the type is not found.

GetType(String, Boolean)

获取程序集实例中具有指定名称的 Type 对象,并选择在找不到该类型时引发异常。Gets the Type object with the specified name in the assembly instance and optionally throws an exception if the type is not found.

GetType(String)

获取程序集实例中具有指定名称的 Type 对象。Gets the Type object with the specified name in the assembly instance.

GetType()

GetType(String, Boolean, Boolean)

获取程序集实例中具有指定名称的 Type 对象,带有忽略大小写和在找不到该类型时引发异常的选项。Gets the Type object with the specified name in the assembly instance, with the options of ignoring the case, and of throwing an exception if the type is not found.

public:
 virtual Type ^ GetType(System::String ^ name, bool throwOnError, bool ignoreCase);
public Type GetType (string name, bool throwOnError, bool ignoreCase);
override this.GetType : string * bool * bool -> Type
Public Function GetType (name As String, throwOnError As Boolean, ignoreCase As Boolean) As Type

参数

name
String

类型的全名。The full name of the type.

throwOnError
Boolean

true 表示在找不到该类型时引发异常;false 则表示返回 nulltrue to throw an exception if the type is not found; false to return null.

ignoreCase
Boolean

如果为 true,则忽略类型名的大小写;否则,为 falsetrue to ignore the case of the type name; otherwise, false.

返回

表示指定类的对象。An object that represents the specified class.

实现

异常

name 无效。name is invalid.

-or- name 的长度超过 1024 个字符。The length of name exceeds 1024 characters.

namenullname is null.

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

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

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

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

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

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

注解

此方法只搜索当前程序集实例。This method only searches the current assembly instance. name 参数包括命名空间,但不包括程序集。The name parameter includes the namespace but not the assembly. 若要搜索类型的其他程序集,请使用 Type.GetType(String) 方法重载,该重载可以选择包含程序集显示名称作为类型名称的一部分。To search other assemblies for a type, use the Type.GetType(String) method overload, which can optionally include an assembly display name as part of the type name.

备注

如果已将该类型转发给另一个程序集,则此方法仍将返回该程序集。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.

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

GetType(String, Boolean)

获取程序集实例中具有指定名称的 Type 对象,并选择在找不到该类型时引发异常。Gets the Type object with the specified name in the assembly instance and optionally throws an exception if the type is not found.

public:
 virtual Type ^ GetType(System::String ^ name, bool throwOnError);
public virtual Type GetType (string name, bool throwOnError);
override this.GetType : string * bool -> Type
Public Overridable Function GetType (name As String, throwOnError As Boolean) As Type

参数

name
String

类型的全名。The full name of the type.

throwOnError
Boolean

true 表示在找不到该类型时引发异常;false 则表示返回 nulltrue to throw an exception if the type is not found; false to return null.

返回

表示指定类的对象。An object that represents the specified class.

实现

异常

name 无效。name is invalid.

-or- name 的长度超过 1024 个字符。The length of name exceeds 1024 characters.

namenullname is null.

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

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

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

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

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

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

注解

此方法只搜索当前程序集实例。This method only searches the current assembly instance. name 参数包括命名空间,但不包括程序集。The name parameter includes the namespace but not the assembly. 若要搜索类型的其他程序集,请使用 Type.GetType(String) 方法重载,该重载可以选择包含程序集显示名称作为类型名称的一部分。To search other assemblies for a type, use the Type.GetType(String) method overload, which can optionally include an assembly display name as part of the type name.

备注

如果已将该类型转发给另一个程序集,则此方法仍将返回该程序集。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.

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

GetType(String)

获取程序集实例中具有指定名称的 Type 对象。Gets the Type object with the specified name in the assembly instance.

public:
 virtual Type ^ GetType(System::String ^ name);
public virtual Type GetType (string name);
override this.GetType : string -> Type
Public Overridable Function GetType (name As String) As Type

参数

name
String

类型的全名。The full name of the type.

返回

表示指定类的对象,若未找到该类则为 nullAn object that represents the specified class, or null if the class is not found.

实现

异常

name 无效。name is invalid.

namenullname is null.

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

适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获基类异常 IOExceptionIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, IOException, instead.

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

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

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

示例

下面的示例在 Transportation 命名空间中定义抽象 MeansOfTransportation 类。The following example defines an abstract MeansOfTransportation class in the Transportation namespace. 它调用 GetType(String) 方法检索其 Type 对象,调用 Type.GetProperties 方法获取表示该类型的属性的 PropertyInfo 对象的数组,然后显示有关该类型的抽象属性的信息。It calls the GetType(String) method to retrieve its Type object, calls the Type.GetProperties method to get an array of PropertyInfo objects that represent the type's properties, and then displays information on the type's abstract properties. 请注意,对 GetType(String) 方法的调用使用类型的完全限定名称(即其命名空间及其类型名称)。Note that the call to the GetType(String) method uses the type's fully qualified name (that is, its namespace along with its type name).

using System;
using System.Reflection;

public class Example
{
    public static void Main()
    {
        Assembly assem = typeof(Example).Assembly;
        Type t = assem.GetType("Transportation.MeansOfTransportation");
        if (t != null)
        {
            Console.WriteLine($"Virtual properties in type {t.FullName}:");
            PropertyInfo[] props = t.GetProperties();
            int nVirtual = 0;
            for (int ctr = 0; ctr < props.Length; ctr++)
            {
                if (props[ctr].GetMethod.IsVirtual)
                {
                    Console.WriteLine($"   {props[ctr].Name} (type {props[ctr].PropertyType.FullName})");
                    nVirtual++;
                }
            }

            if (nVirtual == 0)
                Console.WriteLine("   No virtual properties");
        }
    }
}

namespace Transportation
{
    public abstract class MeansOfTransportation
    {
        abstract public bool HasWheels { get; set; }
        abstract public int Wheels { get; set; }
        abstract public bool ConsumesFuel { get; set; }
        abstract public bool Living { get; set; }
    }

}
// The example displays the following output:
//    Virtual properties in type Transportation.MeansOfTransportation:
//       HasWheels (type System.Boolean)
//       Wheels (type System.Int32)
//       ConsumesFuel (type System.Boolean)
//       Living (type System.Boolean)
Imports System.Reflection

Module Example
   Public Sub Main()
      Dim assem As Assembly = GetType(Example).Assembly
      Dim t As Type = assem.GetType("Transportation.MeansOfTransportation")
      If Not t Is Nothing Then
         Console.WriteLine("Virtual properties in type {0}:", 
                           t.FullName)
         Dim props() As PropertyInfo = t.GetProperties()
         Dim nVirtual As Integer = 0
         For ctr As Integer = 0 To props.Length - 1
            If props(ctr).GetMethod.IsVirtual Then
               Console.WriteLine("   {0} (type {1})",
                                 props(ctr).Name, 
                                 props(ctr).PropertyType.FullName)
               nVirtual += 1
            End If
         Next
         If nVirtual = 0 Then 
            Console.WriteLine("   No virtual properties")
         End If   
      End If   
   End Sub
End Module

Namespace Transportation
   Public MustInherit Class MeansOfTransportation
      Public MustOverride Property HasWheels As Boolean
      Public MustOverride Property Wheels As Integer
      Public MustOverride Property ConsumesFuel As Boolean
      Public MustOverride Property Living As Boolean
   End Class
End Namespace
' The example displays the following output:
'    Virtual properties in type Transportation.MeansOfTransportation:
'       HasWheels (type System.Boolean)
'       Wheels (type System.Int32)
'       ConsumesFuel (type System.Boolean)
'       Living (type System.Boolean)

注解

此方法只搜索当前程序集实例。This method only searches the current assembly instance. name 参数包括命名空间,但不包括程序集。The name parameter includes the namespace but not the assembly. 若要搜索类型的其他程序集,请使用 Type.GetType(String) 方法重载,该重载可以选择包含程序集显示名称作为类型名称的一部分。To search other assemblies for a type, use the Type.GetType(String) method overload, which can optionally include an assembly display name as part of the type name.

备注

如果已将该类型转发给另一个程序集,则此方法仍将返回该程序集。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.

GetType()

public:
 virtual Type ^ GetType();
public Type GetType ();
override this.GetType : unit -> Type
Public Function GetType () As Type

返回

实现

适用于