Assembly.GetType 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取表示指定类型的 Type 对象。
重载
| GetType(String, Boolean, Boolean) | 获取程序集实例中具有指定名称的 Type 对象,带有忽略大小写和在找不到该类型时引发异常的选项。 | 
| GetType(String, Boolean) | 获取程序集实例中具有指定名称的 Type 对象,并选择在找不到该类型时引发异常。 | 
| GetType(String) | 获取程序集实例中具有指定名称的 Type 对象。 | 
| GetType() | 
GetType(String, Boolean, Boolean)
- Source:
- Assembly.cs
- Source:
- Assembly.cs
- Source:
- Assembly.cs
获取程序集实例中具有指定名称的 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 -> TypePublic Overridable Function GetType (name As String, throwOnError As Boolean, ignoreCase As Boolean) As TypePublic 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 为 null。
              throwOnError 为 true,且找不到此类型。
              name 所需的从属程序集无法找到。
              typeName 需要依赖程序集,但文件不是当前加载的运行时的有效程序集。
注解
此方法仅搜索当前程序集实例。 参数 name 包括 命名空间,但不包括程序集。 若要搜索类型的其他程序集,请使用 Type.GetType(String) 方法重载,该方法可以选择将程序集显示名称作为类型名称的一部分。
注意
如果类型已转发到另一个程序集,此方法仍返回该类型。 有关类型转发的信息,请参阅 公共语言运行时中的类型转发。
参数 throwOnError 仅影响找不到类型时发生的情况。 它不会影响可能引发的任何其他异常。 具体而言,如果找到类型但无法加载, TypeLoadException 则即使 throwOnError 为 false,也可以引发 。
适用于
GetType(String, Boolean)
- Source:
- Assembly.cs
- Source:
- Assembly.cs
- Source:
- Assembly.cs
获取程序集实例中具有指定名称的 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 -> TypePublic Overridable Function GetType (name As String, throwOnError As Boolean) As Type参数
- name
- String
类型的全名。
- throwOnError
- Boolean
              true 表示在找不到该类型时引发异常;false 则表示返回 null。
返回
表示指定类的对象。
实现
例外
              name 为 null。
              throwOnError 为 true,且找不到此类型。
              name 所需的从属程序集无法找到。
              typeName 需要依赖程序集,但文件不是当前加载的运行时的有效程序集。
注解
此方法仅搜索当前程序集实例。 参数 name 包括 命名空间,但不包括程序集。 若要搜索类型的其他程序集,请使用 Type.GetType(String) 方法重载,该方法可以选择将程序集显示名称作为类型名称的一部分。
注意
如果类型已转发到另一个程序集,此方法仍返回该类型。 有关类型转发的信息,请参阅 公共语言运行时中的类型转发。
参数 throwOnError 仅影响找不到类型时发生的情况。 它不会影响可能引发的任何其他异常。 具体而言,如果找到类型但无法加载, TypeLoadException 则即使 throwOnError 为 false,也可以引发 。
适用于
GetType(String)
- Source:
- Assembly.cs
- Source:
- Assembly.cs
- Source:
- Assembly.cs
获取程序集实例中具有指定名称的 Type 对象。
public:
 virtual Type ^ GetType(System::String ^ name);public virtual Type GetType (string name);public virtual Type? GetType (string name);override this.GetType : string -> TypePublic Overridable Function GetType (name As String) As Type参数
- name
- String
类型的全名。
返回
表示指定类的对象,若未找到该类则为 null。
实现
例外
              name 无效。
              name 为 null。
              name 所需的从属程序集无法找到。
              name 所需的从属程序集已找到,但无法加载。
- 或 -
当前程序集被加载到仅反射上下文中,并且 name 所需的从属程序集未预先加载。
注意:在 .NET for Windows 应用商店应用 或 可移植类库中,请改为捕获基类异常 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) 方法重载,该方法可以选择将程序集显示名称作为类型名称的一部分。
注意
如果类型已转发到另一个程序集,此方法仍返回该类型。 有关类型转发的信息,请参阅 公共语言运行时中的类型转发。