Assembly.CreateInstance メソッド

定義

このアセンブリから型を検索し、システム アクティベーターを使用してこの型のインスタンスを作成します。

オーバーロード

CreateInstance(String)

大文字小文字を区別する検索を使用してこのアセンブリから指定された型を検索し、システム アクティベーターを使用してこの型のインスタンスを作成します。

CreateInstance(String, Boolean)

オプションの大文字小文字を区別する検索を使用してこのアセンブリから指定された型を検索し、システム アクティベーターを使用してこの型のインスタンスを作成します。

CreateInstance(String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

オプションの大文字小文字を区別する検索を使用して、このアセンブリから指定された型を検索し、システム アクティベーターを使用してこの型のインスタンスを作成し、指定されたカルチャ設定、引数、バインディング属性、およびアクティベーション属性を設定します。

CreateInstance(String)

ソース:
Assembly.cs
ソース:
Assembly.cs
ソース:
Assembly.cs

大文字小文字を区別する検索を使用してこのアセンブリから指定された型を検索し、システム アクティベーターを使用してこの型のインスタンスを作成します。

public:
 System::Object ^ CreateInstance(System::String ^ typeName);
public:
 virtual System::Object ^ CreateInstance(System::String ^ typeName);
public object? CreateInstance (string typeName);
public object CreateInstance (string typeName);
member this.CreateInstance : string -> obj
abstract member CreateInstance : string -> obj
override this.CreateInstance : string -> obj
Public Function CreateInstance (typeName As String) As Object

パラメーター

typeName
String

検索対象の型の FullName

戻り値

パラメーターのないコンストラクターが使用されて作成された指定した型のインスタンス。typeName が見つからない場合は、null。 型は、既定のバインダーを使用して解決されます。カルチャまたはアクティベーション属性は指定せず、BindingFlagsPublic または Instance に設定します。

実装

例外

typeName は空の文字列 ("") または null 文字で始まる文字列です。

- または -

現在のアセンブリは、リフレクションのみのコンテキストに読み込まれました。

typeNamenullです。

一致するコンストラクターが見つかりませんでした。

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

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

- または -

現在のアセンブリはリフレクションのみのコンテキストに読み込まれましたが、typeName には事前に読み込まれていない依存アセンブリが必要です。

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

次の例では、 クラスを Person 定義し、 メソッドを CreateInstance(String) 呼び出してインスタンス化します。

using System;
using System.Reflection;
using Contoso.Libraries;

namespace Contoso.Libraries
{
   public class Person
   {
      private string _name;

      public Person()
      { }

      public Person(string name)
      {
         this._name = name;
      }

      public string Name
      { get { return this._name; }
        set { this._name = value; } }

      public override string ToString()
      {
         return this._name;
      }
   }
}

public class Example
{
   public static void Main()
   {
      Assembly assem = typeof(Person).Assembly;
      Person p = (Person) assem.CreateInstance("Contoso.Libraries.Person");
      if (! (p == null)) {
         p.Name = "John";
         Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
                           p.GetType().Name, p);
      }
      else {
         Console.WriteLine("Unable to instantiate a Person object.");
      }
   }
}
// The example displays the following output:
//        Instantiated a Person object whose value is 'John'
Imports System.Reflection
Imports Contoso.Libraries

Namespace Contoso.Libraries
   Public Class Person
      Private _name As String 
   
      Public Sub New()
      End Sub 
   
      Public Sub New(name As String)
         Me._name = name
      End Sub 
   
      Public Property Name As String 
         Get 
            Return Me._name
         End Get 
         Set 
            Me._name = value
         End Set 
      End Property 
   
      Public Overrides Function ToString() As String 
         Return Me._name
      End Function 
   End Class
End Namespace 

Module Example
   Public Sub Main()
      Dim assem As Assembly = GetType(Person).Assembly
      Dim p As Person = CType(assem.CreateInstance("Contoso.Libraries.Person"),
                              Person)
      If p IsNot Nothing Then
         p.Name = "John"
         Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
                           p.GetType().Name, p)
      Else
         Console.WriteLine("Unable to instantiate a Person object.")
      End If   
   End Sub
End Module
' The example displays the following output:
'       Instantiated a Person object whose value is 'John'

注釈

ランタイムがインスタンス内でAssemblytypeNameつからない場合は、例外をnullスローする代わりに を返します。 これは、次の場合に発生する可能性があります。

  • 型の完全修飾名を指定していません。

  • 完全修飾型名を指定しましたが、その大文字と小文字が型 Type.FullName のプロパティの大文字と小文字が一致しません。 の大文字と小文字を区別せずに型のフル ネームと比較typeNameするには、 オーバーロードをCreateInstance(String, Boolean)呼び出し、 引数に ignoreCase を指定trueします。

  • 型は現在 Assembly のインスタンスに存在しません。

適用対象

CreateInstance(String, Boolean)

ソース:
Assembly.cs
ソース:
Assembly.cs
ソース:
Assembly.cs

オプションの大文字小文字を区別する検索を使用してこのアセンブリから指定された型を検索し、システム アクティベーターを使用してこの型のインスタンスを作成します。

public:
 System::Object ^ CreateInstance(System::String ^ typeName, bool ignoreCase);
public:
 virtual System::Object ^ CreateInstance(System::String ^ typeName, bool ignoreCase);
public object? CreateInstance (string typeName, bool ignoreCase);
public object CreateInstance (string typeName, bool ignoreCase);
member this.CreateInstance : string * bool -> obj
abstract member CreateInstance : string * bool -> obj
override this.CreateInstance : string * bool -> obj
Public Function CreateInstance (typeName As String, ignoreCase As Boolean) As Object

パラメーター

typeName
String

検索対象の型の FullName

ignoreCase
Boolean

型名の大文字と小文字の違いを無視する場合は true。それ以外の場合は false

戻り値

パラメーターのないコンストラクターが使用されて作成された指定した型のインスタンス。typeName が見つからない場合は、null。 型は、既定のバインダーを使用して解決されます。カルチャまたはアクティベーション属性は指定せず、BindingFlagsPublic または Instance に設定します。

実装

例外

typeName は空の文字列 ("") または null 文字で始まる文字列です。

- または -

現在のアセンブリは、リフレクションのみのコンテキストに読み込まれました。

一致するコンストラクターが見つかりませんでした。

typeNamenullです。

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

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

- または -

現在のアセンブリはリフレクションのみのコンテキストに読み込まれましたが、typeName には事前に読み込まれていない依存アセンブリが必要です。

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

次の例では、Person クラスを定義しています。 その後、 メソッドをCreateInstance(String)呼び出してインスタンス化しますが、引数の大文字と小文字が型FullNametypeNameプロパティと一致しないため、 メソッドは を返しますnull。 この例では、同じ文字列をオーバーロードに CreateInstance(String, Boolean) 渡し、比較で大文字と小文字を区別しないことを指定すると、 Person クラスが見つかり Person 、オブジェクトが正常にインスタンス化されます。

using System;
using System.Reflection;
using Contoso.Libraries;

namespace Contoso.Libraries
{
   public class Person
   {
      private string _name;

      public Person()
      { }

      public Person(string name)
      {
         this._name = name;
      }

      public string Name
      { get { return this._name; }
        set { this._name = value; } }

      public override string ToString()
      {
         return this._name;
      }
   }
}

public class Example
{
   public static void Main()
   {
      String fullName = "contoso.libraries.person";
      Assembly assem = typeof(Person).Assembly;
      Person p = (Person) assem.CreateInstance(fullName);
      if (! (p == null)) {
         p.Name = "John";
         Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
                           p.GetType().Name, p);
      }
      else {
         Console.WriteLine("Unable to instantiate a Person object " +
                           "with Assembly.CreateInstance(String)");
         // Try case-insensitive type name comparison.
         p = (Person) assem.CreateInstance(fullName, true);
         if (! (p == null)) {
            p.Name = "John";
            Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
                              p.GetType().Name, p);
         }
         else {
            Console.WriteLine("Unable to instantiate a {0} object.",
                              fullName);
         }
      }
   }
}
// The example displays the following output:
//    Unable to instantiate a Person object with Assembly.CreateInstance(String)
//    Instantiated a Person object whose value is 'John'
Imports System.Reflection
Imports Contoso.Libraries

Namespace Contoso.Libraries
   Public Class Person
      Private _name As String 
   
      Public Sub New()
      End Sub 
   
      Public Sub New(name As String)
         Me._name = name
      End Sub 
   
      Public Property Name As String 
         Get 
            Return Me._name
         End Get 
         Set 
            Me._name = value
         End Set 
      End Property 
   
      Public Overrides Function ToString() As String 
         Return Me._name
      End Function 
   End Class
End Namespace 

Module Example
   Public Sub Main()
      Dim fullName As String = "contoso.libraries.person"
      Dim assem As Assembly = GetType(Person).Assembly
      Dim p As Person = CType(assem.CreateInstance(fullName),
                              Person)
      If p IsNot Nothing Then
         p.Name = "John"
         Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
                           p.GetType().Name, p)
      Else
         Console.WriteLine("Unable to instantiate a Person object" +
                           "with Assembly.CreateInstance(String)")
         ' Try case-insensitive type name comparison.
         p = CType(assem.CreateInstance(fullName, true), Person)
         If p IsNot Nothing Then 
            p.Name = "John"
            Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
                              p.GetType().Name, p)
         Else 
            Console.WriteLine("Unable to instantiate a {0} object.", 
                              fullName)
         End If   
      End If   
   End Sub
End Module
' The example displays the following output:
'    Unable to instantiate a Person object with Assembly.CreateInstance(String)
'    Instantiated a Person object whose value is 'John'

注釈

ランタイムがインスタンス内でAssemblytypeNameつからない場合は、例外をnullスローする代わりに を返します。 これは、次の場合に発生する可能性があります。

  • 型の完全修飾名を指定していません。

  • 型は現在 Assembly のインスタンスに存在しません。

適用対象

CreateInstance(String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

ソース:
Assembly.cs
ソース:
Assembly.cs
ソース:
Assembly.cs

オプションの大文字小文字を区別する検索を使用して、このアセンブリから指定された型を検索し、システム アクティベーターを使用してこの型のインスタンスを作成し、指定されたカルチャ設定、引数、バインディング属性、およびアクティベーション属性を設定します。

public:
 virtual System::Object ^ CreateInstance(System::String ^ typeName, bool ignoreCase, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ args, System::Globalization::CultureInfo ^ culture, cli::array <System::Object ^> ^ activationAttributes);
public virtual object? CreateInstance (string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, object[]? args, System.Globalization.CultureInfo? culture, object[]? activationAttributes);
public virtual object CreateInstance (string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes);
public object CreateInstance (string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes);
abstract member CreateInstance : string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] -> obj
override this.CreateInstance : string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] -> obj
Public Overridable Function CreateInstance (typeName As String, ignoreCase As Boolean, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo, activationAttributes As Object()) As Object
Public Function CreateInstance (typeName As String, ignoreCase As Boolean, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo, activationAttributes As Object()) As Object

パラメーター

typeName
String

検索対象の型の FullName

ignoreCase
Boolean

型名の大文字と小文字の違いを無視する場合は true。それ以外の場合は false

bindingAttr
BindingFlags

検索の実行方法に影響を与えるビットマスク。 値は、BindingFlags のビット フラグの組み合わせです。

binder
Binder

バインディング、引数型の強制変換、メンバーの呼び出し、および、リフレクションによる MemberInfo オブジェクトの取得を有効にするオブジェクト。 bindernull の場合は、既定のバインダーが使用されます。

args
Object[]

コンストラクターに渡す引数を格納する配列。 この引数の配列は、呼び出すコンストラクターのパラメーターと、数、順序、および型が同じでなければなりません。 パラメーターなしのコンストラクターを使用する場合は、args を空の配列または null にする必要があります。

culture
CultureInfo

型の強制変換を制御するために使用する CultureInfo のインスタンス。 null の場合は、現在のスレッドの CultureInfo が使用されます。 (これは、たとえば、1000 を表す文字列を Double 値に変換する場合に必要です。これは、カルチャによって 1000 の表記が異なるためです。)

activationAttributes
Object[]

アクティべーションに参加できる 1 つ以上の属性の配列。 通常、リモート オブジェクトをアクティブ化するために必要な URL を指定する 1 つの UrlAttribute オブジェクトを含む配列。 このパラメーターは、クライアント側でアクティブ化されるオブジェクトに関連しています。 クライアント アクティベーションは、下位互換性のために残されているレガシ テクノロジです。新規の開発には使用しないでください。 分散アプリケーションは、代わりに Windows Communication Foundation (WCF) を使用する必要があります。

戻り値

指定した型のインスタンス。typeName が見つからない場合は、null。 指定された引数は、型を解決するため、およびインスタンスの作成に使用されるコンストラクターをバインドするために使用されます。

実装

例外

typeName は空の文字列 ("") または null 文字で始まる文字列です。

- または -

現在のアセンブリは、リフレクションのみのコンテキストに読み込まれました。

typeNamenullです。

一致するコンストラクターが見つかりませんでした。

空でないアクティベーション属性の配列は、 MarshalByRefObjectから継承されていない型に渡されます。

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

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

- または -

現在のアセンブリはリフレクションのみのコンテキストに読み込まれましたが、typeName には事前に読み込まれていない依存アセンブリが必要です。

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

適用対象