Assembly.GetType メソッド

定義

指定した型を表す Type オブジェクトを取得します。

オーバーロード

名前 説明
GetType(String, Boolean, Boolean)

アセンブリ インスタンス内の指定した名前を持つ Type オブジェクトを取得します。大文字と小文字を区別せず、型が見つからない場合は例外をスローするオプションを指定します。

GetType(String, Boolean)

アセンブリ インスタンス内の指定した名前を持つ Type オブジェクトを取得し、必要に応じて、型が見つからない場合は例外をスローします。

GetType()
GetType(String)

アセンブリ インスタンス内の指定した名前を持つ Type オブジェクトを取得します。

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 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型が見つからない場合に例外をスローする場合。nullを返すfalse

ignoreCase
Boolean

true 型名の大文字と小文字が区別されません。それ以外の場合は false

返品

指定したクラスを表すオブジェクト。

実装

例外

name が無効です。

-または-

nameの長さが 1024 文字を超えています。

namenullです。

throwOnErrortrueであり、型が見つかりません。

name には、見つからなかった依存アセンブリが必要です。

name には、見つかったが読み込めなかった依存アセンブリが必要です。

-または-

現在のアセンブリはリフレクションのみのコンテキストに読み込まれ、 name にはプリロードされていない依存アセンブリが必要です。

name には依存アセンブリが必要ですが、ファイルは現在読み込まれているランタイムの有効なアセンブリではありません。

注釈

このメソッドは、現在のアセンブリ インスタンスのみを検索します。 name パラメーターには名前空間が含まれますが、アセンブリは含まれません。 他のアセンブリで型を検索するには、 Type.GetType(String) メソッドオーバーロードを使用します。このオーバーロードには、必要に応じて、アセンブリの表示名を型名の一部として含めることができます。

.NET Core/.NET 5 以降では、型名文字列にアセンブリ修飾ジェネリック型パラメーターがある場合、アセンブリ参照は Assembly.GetType を呼び出したメソッドの AssemblyLoadContext、または設定されている場合は AssemblyLoadContext.CurrentContextualReflectionContext コンテキストによって読み込まれます。

Note

型が別のアセンブリに転送された場合でも、このメソッドによって返されます。 型転送の詳細については、「 共通言語ランタイムでの型転送」を参照してください。

throwOnError パラメーターは、型が見つからない場合の動作にのみ影響します。 スローされる可能性のある他の例外には影響しません。 特に、型が見つかったが読み込めない場合は、throwOnErrorfalseされた場合でも、TypeLoadExceptionをスローできます。

適用対象

GetType(String, Boolean)

アセンブリ インスタンス内の指定した名前を持つ Type オブジェクトを取得し、必要に応じて、型が見つからない場合は例外をスローします。

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

型の完全な名前。

throwOnError
Boolean

true型が見つからない場合に例外をスローする場合。nullを返すfalse

返品

指定したクラスを表すオブジェクト。

実装

例外

name が無効です。

-または-

nameの長さが 1024 文字を超えています。

namenullです。

throwOnErrortrueであり、型が見つかりません。

name には、見つからなかった依存アセンブリが必要です。

name には、見つかったが読み込めなかった依存アセンブリが必要です。

-または-

現在のアセンブリはリフレクションのみのコンテキストに読み込まれ、 name にはプリロードされていない依存アセンブリが必要です。

name には依存アセンブリが必要ですが、ファイルは現在読み込まれているランタイムの有効なアセンブリではありません。

注釈

このメソッドは、現在のアセンブリ インスタンスのみを検索します。 name パラメーターには名前空間が含まれますが、アセンブリは含まれません。 他のアセンブリで型を検索するには、 Type.GetType(String) メソッドオーバーロードを使用します。このオーバーロードには、必要に応じて、アセンブリの表示名を型名の一部として含めることができます。

.NET Core/.NET 5 以降では、型名文字列にアセンブリ修飾ジェネリック型パラメーターがある場合、アセンブリ参照は Assembly.GetType を呼び出したメソッドの AssemblyLoadContext、または設定されている場合は AssemblyLoadContext.CurrentContextualReflectionContext コンテキストによって読み込まれます。

Note

型が別のアセンブリに転送された場合でも、このメソッドによって返されます。 型転送の詳細については、「 共通言語ランタイムでの型転送」を参照してください。

throwOnError パラメーターは、型が見つからない場合の動作にのみ影響します。 スローされる可能性のある他の例外には影響しません。 特に、型が見つかったが読み込めない場合は、throwOnErrorfalseされた場合でも、TypeLoadExceptionをスローできます。

適用対象

GetType()

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

返品

実装

適用対象

GetType(String)

アセンブリ インスタンス内の指定した名前を持つ Type オブジェクトを取得します。

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

型の完全な名前。

返品

指定したクラスを表すオブジェクト。クラスが見つからない場合は null

実装

例外

name が無効です。

namenullです。

name には、見つからなかった依存アセンブリが必要です。

name には、見つかったが読み込めなかった依存アセンブリが必要です。

-または-

現在のアセンブリはリフレクションのみのコンテキストに読み込まれ、 name にはプリロードされていない依存アセンブリが必要です。

name には依存アセンブリが必要ですが、ファイルは現在読み込まれているランタイムの有効なアセンブリではありません。

次の例では、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) メソッドオーバーロードを使用します。このオーバーロードには、必要に応じて、アセンブリの表示名を型名の一部として含めることができます。

.NET Core/.NET 5 以降では、型名文字列にアセンブリ修飾ジェネリック型パラメーターがある場合、アセンブリ参照は Assembly.GetType を呼び出したメソッドの AssemblyLoadContext、または設定されている場合は AssemblyLoadContext.CurrentContextualReflectionContext コンテキストによって読み込まれます。

Note

型が別のアセンブリに転送された場合でも、このメソッドによって返されます。 型転送の詳細については、「 共通言語ランタイムでの型転送」を参照してください。

適用対象