Assembly.CreateInstance 方法

定義

從這個組件找出類型,並使用系統啟動項 (Activator) 建立它的執行個體。

多載

CreateInstance(String)

從這個組件找出指定類型,並使用系統啟動項,利用區分大小寫的搜尋,建立它的執行個體。

CreateInstance(String, Boolean)

從這個組件找出指定類型,並使用系統啟動項,利用選擇性區分大小寫的搜尋,建立它的執行個體。

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

從這個組件找出指定類型,並使用系統啟動項,利用選擇性區分大小寫的搜尋,以及取得指定文化特性 (Culture)、引數和繫結和啟動屬性,建立它的執行個體。

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。 該類型在 BindingFlags 設定為 PublicInstance 時,於未指定文化特性或啟動屬性的情況下,使用預設繫結器進行解析。

實作

例外狀況

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'

備註

如果運行時間在 實例中Assembly找不到typeName,它會傳回而不是擲回null例外狀況。 發生這種情況的可能原因是:

  • 您尚未指定類型的完整名稱。

  • 您已指定完整型別名稱,但其大小寫與類型 Type.FullName 屬性的大小寫不符。 若為與型別的完整名稱不區分大小寫的比較 typeName ,請呼叫 CreateInstance(String, Boolean) 多載並指定 trueignoreCase 變數。

  • 此類型不存在於目前的 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。 該類型在 BindingFlags 設定為 PublicInstance 時,於未指定文化特性或啟動屬性的情況下,使用預設繫結器進行解析。

實作

例外狀況

typeName 為空字串 ("") 或以 null 字元開頭的字串。

-或-

目前的組件已載入到僅限反映的內容中。

找不到相符的建構函式。

typeNamenull

找不到 typeName 所需的相依組件。

找到 typeName 所需的相依組件,但無法載入。

-或-

目前的組件已載入僅限反映的內容中,而且 typeName 需要未預先載入的相依組件。

typeName 需要相依元件,但檔案不是目前載入運行時間的有效元件。

範例

下列範例將定義 Person 類別。 然後它會呼叫 CreateInstance(String) 方法來具現化它,但因為 typeName 自變數的大小寫與類型 FullName 屬性的大小寫不符,所以 方法會傳 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'

備註

如果運行時間在 實例中Assembly找不到typeName,它會傳回而不是擲回null例外狀況。 發生這種情況的可能原因是:

  • 您尚未指定類型的完整名稱。

  • 此類型不存在於目前的 Assembly 實例中。

適用於

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

來源:
Assembly.cs
來源:
Assembly.cs
來源:
Assembly.cs

從這個組件找出指定類型,並使用系統啟動項,利用選擇性區分大小寫的搜尋,以及取得指定文化特性 (Culture)、引數和繫結和啟動屬性,建立它的執行個體。

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[]

一或多個屬性的陣列,此屬性可參與啟動過程。 陣列通常只會包含一個 UrlAttribute 物件來指定用以啟動遠端物件的 URL。 此參數與啟動了用戶端的物件相關。 用戶端啟動是一項舊的技術,保留目的在提供回溯相容性,不建議用於新的開發。 分散式應用程式應該改用 Windows Communication Foundation。

傳回

指定之類型的執行個體,如果找不到 null 則為 typeName。 所提供的引數會用來解析類型,以及繫結建立執行個體所用的建構函式。

實作

例外狀況

typeName 為空字串 ("") 或以 null 字元開頭的字串。

-或-

目前的組件已載入到僅限反映的內容中。

typeNamenull

找不到相符的建構函式。

將非空白的啟用屬性陣列傳遞給不是繼承自 MarshalByRefObject 的類型。

找不到 typeName 所需的相依組件。

找到 typeName 所需的相依組件,但無法載入。

-或-

目前的組件已載入僅限反映的內容中,而且 typeName 需要未預先載入的相依組件。

typeName 需要相依元件,但檔案不是目前載入運行時間的有效元件。

適用於