Assembly.GetType 方法

定义

获取表示指定类型的 Type 对象。

重载

GetType(String, Boolean, Boolean)

获取程序集实例中具有指定名称的 Type 对象,带有忽略大小写和在找不到该类型时引发异常的选项。

GetType(String, Boolean)

获取程序集实例中具有指定名称的 Type 对象,并选择在找不到该类型时引发异常。

GetType(String)

获取程序集实例中具有指定名称的 Type 对象。

GetType()

GetType(String, Boolean, Boolean)

获取程序集实例中具有指定名称的 Type 对象,带有忽略大小写和在找不到该类型时引发异常的选项。

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

参数

name
String

类型的全名。

throwOnError
Boolean

true 表示在找不到该类型时引发异常;false 则表示返回 null

ignoreCase
Boolean

如果为 true,则忽略类型名的大小写;否则,为 false

返回

表示指定类的对象。

实现

例外

name 无效。

- 或 -

name 的长度超过 1024 个字符。

namenull

throwOnErrortrue,且找不到此类型。

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

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

- 或 -

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

typeName 需要依赖程序集,但文件不是当前加载的运行时的有效程序集。

注解

此方法仅搜索当前程序集实例。 参数 name 包括 命名空间,但不包括程序集。 若要在其他程序集中搜索某个类型,请使用 Type.GetType(String) 方法重载,该方法可以选择将程序集显示名称作为类型名称的一部分包含在内。

注意

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

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

适用于

GetType(String, Boolean)

获取程序集实例中具有指定名称的 Type 对象,并选择在找不到该类型时引发异常。

public:
 virtual Type ^ GetType(System::String ^ name, bool throwOnError);
public virtual Type? GetType (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

类型的全名。

throwOnError
Boolean

true 表示在找不到该类型时引发异常;false 则表示返回 null

返回

表示指定类的对象。

实现

例外

name 无效。

- 或 -

name 的长度超过 1024 个字符。

namenull

throwOnErrortrue,且找不到此类型。

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

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

- 或 -

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

typeName 需要依赖程序集,但文件不是当前加载的运行时的有效程序集。

注解

此方法仅搜索当前程序集实例。 参数 name 包括 命名空间,但不包括程序集。 若要在其他程序集中搜索某个类型,请使用 Type.GetType(String) 方法重载,该方法可以选择将程序集显示名称作为类型名称的一部分包含在内。

注意

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

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

适用于

GetType(String)

获取程序集实例中具有指定名称的 Type 对象。

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

参数

name
String

类型的全名。

返回

表示指定类的对象,若未找到该类则为 null

实现

例外

name 无效。

namenull

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

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

- 或 -

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

注意:在 适用于 Windows 应用商店应用的 .NET可移植类库中,改为捕获基类异常 IOException

typeName 需要依赖程序集,但文件不是当前加载的运行时的有效程序集。

示例

以下示例在 命名空间中Transportation定义一个抽象MeansOfTransportation类。 它调用 GetType(String) 方法以检索其 Type 对象,调用 Type.GetProperties 方法以获取表示类型属性的对象数组 PropertyInfo ,然后显示有关该类型的抽象属性的信息。 请注意,对 GetType(String) 方法的调用使用类型的完全限定名称 (即,其命名空间及其类型名称) 。

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)

注解

此方法仅搜索当前程序集实例。 参数 name 包括 命名空间,但不包括程序集。 若要在其他程序集中搜索某个类型,请使用 Type.GetType(String) 方法重载,该方法可以选择将程序集显示名称作为类型名称的一部分包含在内。

注意

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

适用于

GetType()

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

返回

实现

适用于