AppDomain.CreateInstanceFrom 方法

定義

建立指定組件檔案中所定義之指定類型的新執行個體。Creates a new instance of a specified type defined in the specified assembly file.

多載

CreateInstanceFrom(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
已過時。
已過時。

建立指定組件檔案中所定義之指定類型的新執行個體。Creates a new instance of the specified type defined in the specified assembly file.

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

建立指定組件檔案中所定義之指定類型的新執行個體。Creates a new instance of the specified type defined in the specified assembly file.

CreateInstanceFrom(String, String, Object[])

建立指定組件檔案中所定義之指定類型的新執行個體。Creates a new instance of the specified type defined in the specified assembly file.

CreateInstanceFrom(String, String)

建立指定組件檔案中所定義之指定類型的新執行個體。Creates a new instance of the specified type defined in the specified assembly file.

CreateInstanceFrom(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)

警告

Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstanceFrom which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.

警告

Use an overload that does not take an Evidence parameter

建立指定組件檔案中所定義之指定類型的新執行個體。Creates a new instance of the specified type defined in the specified assembly file.

public:
 virtual System::Runtime::Remoting::ObjectHandle ^ CreateInstanceFrom(System::String ^ assemblyFile, 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, System::Security::Policy::Evidence ^ securityAttributes);
public:
 System::Runtime::Remoting::ObjectHandle ^ CreateInstanceFrom(System::String ^ assemblyFile, 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, System::Security::Policy::Evidence ^ securityAttributes);
public System.Runtime.Remoting.ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes, System.Security.Policy.Evidence securityAttributes);
[System.Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstanceFrom which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Runtime.Remoting.ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes, System.Security.Policy.Evidence securityAttributes);
[System.Obsolete("Use an overload that does not take an Evidence parameter")]
public System.Runtime.Remoting.ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes, System.Security.Policy.Evidence securityAttributes);
abstract member CreateInstanceFrom : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] * System.Security.Policy.Evidence -> System.Runtime.Remoting.ObjectHandle
override this.CreateInstanceFrom : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] * System.Security.Policy.Evidence -> System.Runtime.Remoting.ObjectHandle
[<System.Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstanceFrom which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")>]
abstract member CreateInstanceFrom : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] * System.Security.Policy.Evidence -> System.Runtime.Remoting.ObjectHandle
override this.CreateInstanceFrom : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] * System.Security.Policy.Evidence -> System.Runtime.Remoting.ObjectHandle
[<System.Obsolete("Use an overload that does not take an Evidence parameter")>]
member this.CreateInstanceFrom : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] * System.Security.Policy.Evidence -> System.Runtime.Remoting.ObjectHandle
[<System.Obsolete("Use an overload that does not take an Evidence parameter")>]
abstract member CreateInstanceFrom : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] * System.Security.Policy.Evidence -> System.Runtime.Remoting.ObjectHandle
override this.CreateInstanceFrom : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] * System.Security.Policy.Evidence -> System.Runtime.Remoting.ObjectHandle
Public Function CreateInstanceFrom (assemblyFile As String, typeName As String, ignoreCase As Boolean, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo, activationAttributes As Object(), securityAttributes As Evidence) As ObjectHandle

參數

assemblyFile
String

檔案的名稱 (包括路徑),此檔案包含會定義要求之類型的組件。The name, including the path, of a file that contains an assembly that defines the requested type. 此組件使用 LoadFrom(String) 方法載入。The assembly is loaded using the LoadFrom(String) method.

typeName
String

FullName 屬性傳回的要求類型之完整名稱 (包括命名空間,但不包括組件)。The fully qualified name of the requested type, including the namespace but not the assembly, as returned by the FullName property.

ignoreCase
Boolean

布林值,指出是否執行區分大小寫的搜尋。A Boolean value specifying whether to perform a case-sensitive search or not.

bindingAttr
BindingFlags

零或多個位元旗標的組合,此位元旗標會影響 typeName 建構函式的搜尋。A combination of zero or more bit flags that affect the search for the typeName constructor. 如果 bindingAttr 為零,則會針對公用建構函式執行區分大小寫的搜尋。If bindingAttr is zero, a case-sensitive search for public constructors is conducted.

binder
Binder

使用反映來啟用繫結、強制引數的類型、成員的引動過程,和擷取 MemberInfo 物件的物件。An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects through reflection. 如果 binder 為 null,則會使用預設繫結器。If binder is null, the default binder is used.

args
Object[]

要傳遞到建構函式的引數。The arguments to pass to the constructor. 這個引數陣列必須在數目、順序和類型上符合要叫用之建構函式的參數。This array of arguments must match in number, order, and type the parameters of the constructor to invoke. 如果慣用無參數建構函式,則 args 必須是空陣列或 Null。If the parameterless constructor is preferred, args must be an empty array or null.

culture
CultureInfo

特定文化特性的資訊,其可控制 args 到型式類型 (為 typeName 建構函式宣告) 的強制轉型。Culture-specific information that governs the coercion of args to the formal types declared for the typeName constructor. 如果 culturenull,會使用目前執行緒的 CultureInfoIf culture is null, the CultureInfo for the current thread is used.

activationAttributes
Object[]

一或多個屬性的陣列,此屬性可參與啟動過程。An array of one or more attributes that can participate in activation. 陣列通常只會包含一個 UrlAttribute 物件來指定用以啟動遠端物件的 URL。Typically, an array that contains a single UrlAttribute object that specifies the URL that is required to activate a remote object.

此參數與啟動了用戶端的物件相關。This parameter is related to client-activated objects. 用戶端啟動是一項舊的技術,保留目的在提供回溯相容性,不建議用於新的開發。Client activation is a legacy technology that is retained for backward compatibility but is not recommended for new development. 分散式應用程式應該改用 Windows Communication Foundation。Distributed applications should instead use Windows Communication Foundation.

securityAttributes
Evidence

用來授權建立 typeName 的資訊。Information used to authorize creation of typeName.

傳回

ObjectHandle

對新執行個體而言是包裝函式的物件,但如果沒有找到 typeName,則為 nullAn object that is a wrapper for the new instance, or null if typeName is not found. 傳回值需要解除包裝以存取實際物件。The return value needs to be unwrapped to access the real object.

實作

屬性

例外狀況

assemblyFilenullassemblyFile is null.

-或--or- typeNamenulltypeName is null.

呼叫者無法提供非繼承自 MarshalByRefObject 之物件的啟動屬性。The caller cannot provide activation attributes for an object that does not inherit from MarshalByRefObject.

-或--or- securityAttributes 不是 nullsecurityAttributes is not null. 若未啟用舊版 CAS 原則,securityAttributes 應為 nullWhen legacy CAS policy is not enabled, securityAttributes should be null.

嘗試對卸載的應用程式定義域執行作業。The operation is attempted on an unloaded application domain.

找不到 assemblyFileassemblyFile was not found.

assemblyFile 中找不到 typeNametypeName was not found in assemblyFile.

找不到相符的公用建構函式。No matching public constructor was found.

呼叫端沒有足以呼叫這個建構函式的權限。The caller does not have sufficient permission to call this constructor.

assemblyFile 不是有效的組件。assemblyFile is not a valid assembly.

-或--or- 目前載入的是 Common Language Runtime 2.0 版或更新版本,編譯 assemblyFile 的版本還要更新。Version 2.0 or later of the common language runtime is currently loaded and assemblyFile was compiled with a later version.

使用兩個不同的辨識項載入組件或模組兩次。An assembly or module was loaded twice with two different evidences.

這個執行個體是 nullThis instance is null.

備註

如需此方法的詳細資訊,請參閱 Activator.CreateInstanceFrom 方法。For more information about this method, see the Activator.CreateInstanceFrom method.

CreateInstanceFrom 方法用來在目標應用程式域中建立實例時,除了進行呼叫的應用程式域以外,也會在目標應用程式域中載入元件。When the CreateInstanceFrom method is used to create an instance in a target application domain, other than the application domain from which the call is made, the assembly is loaded in the target application domain. 但是,如果實例在呼叫的應用程式域中解除包裝,以特定方式使用未包裝的實例可能會導致元件載入至呼叫的應用程式域。However, if the instance is unwrapped in the calling application domain, using the unwrapped instance in certain ways can cause the assembly to be loaded into the calling application domain. 例如,在實例解除包裝之後,可能會要求其型別資訊,以便在晚期繫結時呼叫其方法。For example, after the instance is unwrapped, its type information might be requested, in order to call its methods late-bound. 將元件載入至呼叫的應用程式域時,可能會發生例外狀況。When the assembly is loaded into the calling application domain, exceptions can occur.

  • 如果先前已將相同元件的另一個版本載入至呼叫的應用程式域,或呼叫應用程式域的載入路徑與目標應用程式域的載入路徑不同,則 MissingMethodException 可能會發生例外狀況。If another version of the same assembly was previously loaded into the calling application domain, or if the load path of the calling application domain is different from that of the target application domain, exceptions such as MissingMethodException can occur.

  • 如果呼叫的應用程式域對實例型別進行早期繫結的呼叫, InvalidCastException 就可以在嘗試轉換實例時擲回。If the calling application domain makes early-bound calls to the instance type, InvalidCastException can be thrown when an attempt is made to cast the instance.

另請參閱

適用於

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

建立指定組件檔案中所定義之指定類型的新執行個體。Creates a new instance of the specified type defined in the specified assembly file.

public:
 System::Runtime::Remoting::ObjectHandle ^ CreateInstanceFrom(System::String ^ assemblyFile, 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 System.Runtime.Remoting.ObjectHandle CreateInstanceFrom (string? assemblyFile, string? typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes);
public System.Runtime.Remoting.ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes);
member this.CreateInstanceFrom : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] -> System.Runtime.Remoting.ObjectHandle
Public Function CreateInstanceFrom (assemblyFile As String, typeName As String, ignoreCase As Boolean, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo, activationAttributes As Object()) As ObjectHandle

參數

assemblyFile
String

檔案的名稱 (包括路徑),此檔案包含會定義要求之類型的組件。The name, including the path, of a file that contains an assembly that defines the requested type. 此組件使用 LoadFrom(String) 方法載入。The assembly is loaded using the LoadFrom(String) method.

typeName
String

FullName 屬性傳回的要求類型之完整名稱 (包括命名空間,但不包括組件)。The fully qualified name of the requested type, including the namespace but not the assembly, as returned by the FullName property.

ignoreCase
Boolean

布林值,指出是否執行區分大小寫的搜尋。A Boolean value specifying whether to perform a case-sensitive search or not.

bindingAttr
BindingFlags

零或多個位元旗標的組合,此位元旗標會影響 typeName 建構函式的搜尋。A combination of zero or more bit flags that affect the search for the typeName constructor. 如果 bindingAttr 為零,則會針對公用建構函式執行區分大小寫的搜尋。If bindingAttr is zero, a case-sensitive search for public constructors is conducted.

binder
Binder

使用反映來啟用繫結、強制引數的類型、成員的引動過程,和擷取 MemberInfo 物件的物件。An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects through reflection. 如果 binder 為 null,則會使用預設繫結器。If binder is null, the default binder is used.

args
Object[]

要傳遞到建構函式的引數。The arguments to pass to the constructor. 這個引數陣列必須在數目、順序和類型上符合要叫用之建構函式的參數。This array of arguments must match in number, order, and type the parameters of the constructor to invoke. 如果慣用無參數建構函式,則 args 必須是空陣列或 Null。If the parameterless constructor is preferred, args must be an empty array or null.

culture
CultureInfo

特定文化特性的資訊,其可控制 args 到型式類型 (為 typeName 建構函式宣告) 的強制轉型。Culture-specific information that governs the coercion of args to the formal types declared for the typeName constructor. 如果 culturenull,會使用目前執行緒的 CultureInfoIf culture is null, the CultureInfo for the current thread is used.

activationAttributes
Object[]

一或多個屬性的陣列,此屬性可參與啟動過程。An array of one or more attributes that can participate in activation. 陣列通常只會包含一個 UrlAttribute 物件來指定用以啟動遠端物件的 URL。Typically, an array that contains a single UrlAttribute object that specifies the URL that is required to activate a remote object.

此參數與啟動了用戶端的物件相關。This parameter is related to client-activated objects. 用戶端啟動是一項舊的技術,保留目的在提供回溯相容性,不建議用於新的開發。Client activation is a legacy technology that is retained for backward compatibility but is not recommended for new development. 分散式應用程式應該改用 Windows Communication Foundation。Distributed applications should instead use Windows Communication Foundation.

傳回

ObjectHandle

對新執行個體而言是包裝函式的物件,但如果沒有找到 typeName,則為 nullAn object that is a wrapper for the new instance, or null if typeName is not found. 傳回值需要解除包裝以存取實際物件。The return value needs to be unwrapped to access the real object.

例外狀況

assemblyFilenullassemblyFile is null.

-或--or- typeNamenulltypeName is null.

呼叫者無法提供非繼承自 MarshalByRefObject 之物件的啟動屬性。The caller cannot provide activation attributes for an object that does not inherit from MarshalByRefObject.

嘗試對卸載的應用程式定義域執行作業。The operation is attempted on an unloaded application domain.

找不到 assemblyFileassemblyFile was not found.

assemblyFile 中找不到 typeNametypeName was not found in assemblyFile.

找不到相符的公用建構函式。No matching public constructor was found.

呼叫端沒有足以呼叫這個建構函式的權限。The caller does not have sufficient permission to call this constructor.

assemblyFile 不是有效的組件。assemblyFile is not a valid assembly.

-或--or- 用以編譯 assemblyFile 的通用語言執行平台,其版本比目前所載入的版本還要更新。assemblyFile was compiled with a later version of the common language runtime than the version that is currently loaded.

使用兩個不同的辨識項載入組件或模組兩次。An assembly or module was loaded twice with two different evidences.

這個執行個體是 nullThis instance is null.

備註

如需詳細資訊,請參閱 Activator.CreateInstanceFrom 方法。For more information, see the Activator.CreateInstanceFrom method.

CreateInstanceFrom 方法用來在目標應用程式域中建立實例時,除了進行呼叫的應用程式域以外,也會在目標應用程式域中載入元件。When the CreateInstanceFrom method is used to create an instance in a target application domain, other than the application domain from which the call is made, the assembly is loaded in the target application domain. 但是,如果實例在呼叫的應用程式域中解除包裝,以特定方式使用未包裝的實例可能會導致元件載入至呼叫的應用程式域。However, if the instance is unwrapped in the calling application domain, using the unwrapped instance in certain ways can cause the assembly to be loaded into the calling application domain. 例如,在實例解除包裝之後,可能會要求其型別資訊,以便在晚期繫結時呼叫其方法。For example, after the instance is unwrapped, its type information might be requested, in order to call its methods late-bound. 將元件載入至呼叫的應用程式域時,可能會發生例外狀況。When the assembly is loaded into the calling application domain, exceptions can occur.

  • 如果先前已將相同元件的另一個版本載入至呼叫的應用程式域,或呼叫應用程式域的載入路徑與目標應用程式域的載入路徑不同,則 MissingMethodException 可能會發生例外狀況。If another version of the same assembly was previously loaded into the calling application domain, or if the load path of the calling application domain is different from that of the target application domain, exceptions such as MissingMethodException can occur.

  • 如果呼叫的應用程式域對實例型別進行早期繫結的呼叫, InvalidCastException 就可以在嘗試轉換實例時擲回。If the calling application domain makes early-bound calls to the instance type, InvalidCastException can be thrown when an attempt is made to cast the instance.

另請參閱

適用於

CreateInstanceFrom(String, String, Object[])

建立指定組件檔案中所定義之指定類型的新執行個體。Creates a new instance of the specified type defined in the specified assembly file.

public:
 System::Runtime::Remoting::ObjectHandle ^ CreateInstanceFrom(System::String ^ assemblyFile, System::String ^ typeName, cli::array <System::Object ^> ^ activationAttributes);
public:
 virtual System::Runtime::Remoting::ObjectHandle ^ CreateInstanceFrom(System::String ^ assemblyFile, System::String ^ typeName, cli::array <System::Object ^> ^ activationAttributes);
public System.Runtime.Remoting.ObjectHandle? CreateInstanceFrom (string assemblyFile, string typeName, object[]? activationAttributes);
public System.Runtime.Remoting.ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName, object[] activationAttributes);
member this.CreateInstanceFrom : string * string * obj[] -> System.Runtime.Remoting.ObjectHandle
abstract member CreateInstanceFrom : string * string * obj[] -> System.Runtime.Remoting.ObjectHandle
override this.CreateInstanceFrom : string * string * obj[] -> System.Runtime.Remoting.ObjectHandle
Public Function CreateInstanceFrom (assemblyFile As String, typeName As String, activationAttributes As Object()) As ObjectHandle

參數

assemblyFile
String

檔案的名稱 (包括路徑),此檔案包含會定義要求之類型的組件。The name, including the path, of a file that contains an assembly that defines the requested type. 此組件使用 LoadFrom(String) 方法載入。The assembly is loaded using the LoadFrom(String) method.

typeName
String

FullName 屬性傳回的要求類型之完整名稱 (包括命名空間,但不包括組件)。The fully qualified name of the requested type, including the namespace but not the assembly, as returned by the FullName property.

activationAttributes
Object[]

一或多個屬性的陣列,此屬性可參與啟動過程。An array of one or more attributes that can participate in activation. 陣列通常只會包含一個 UrlAttribute 物件來指定用以啟動遠端物件的 URL。Typically, an array that contains a single UrlAttribute object that specifies the URL that is required to activate a remote object.

此參數與 client-activated 物件有關。用戶端啟動是一項舊的技術,保留目的在提供回溯相容性,不建議用於新的開發。This parameter is related to client-activated objects.Client activation is a legacy technology that is retained for backward compatibility but is not recommended for new development. 分散式應用程式應該改用 Windows Communication Foundation。Distributed applications should instead use Windows Communication Foundation.

傳回

ObjectHandle

對新執行個體而言是包裝函式的物件,但如果沒有找到 typeName,則為 nullAn object that is a wrapper for the new instance, or null if typeName is not found. 傳回值需要解除包裝以存取實際物件。The return value needs to be unwrapped to access the real object.

實作

例外狀況

assemblyFilenullassemblyFile is null.

找不到 assemblyFileassemblyFile was not found.

assemblyFile 中找不到 typeNametypeName was not found in assemblyFile.

呼叫端沒有足以呼叫這個建構函式的權限。The caller does not have sufficient permission to call this constructor.

找不到相符的公用建構函式。No matching public constructor was found.

呼叫者無法提供非繼承自 MarshalByRefObject 之物件的啟動屬性。The caller cannot provide activation attributes for an object that does not inherit from MarshalByRefObject.

嘗試對卸載的應用程式定義域執行作業。The operation is attempted on an unloaded application domain.

assemblyFile 不是有效的組件。assemblyFile is not a valid assembly.

-或--or- 目前載入的是 Common Language Runtime 2.0 版或更新版本,編譯 assemblyFile 的版本還要更新。Version 2.0 or later of the common language runtime is currently loaded and assemblyFile was compiled with a later version.

使用兩個不同的辨識項載入組件或模組兩次。An assembly or module was loaded twice with two different evidences.

這個執行個體是 nullThis instance is null.

備註

叫用的無參數函數 typeNameThe parameterless constructor for typeName is invoked.

如需此方法的詳細資訊,請參閱 Activator.CreateInstanceFrom 方法。For more information about this method, see the Activator.CreateInstanceFrom method.

CreateInstanceFrom 方法用來在目標應用程式域中建立實例時,除了進行呼叫的應用程式域以外,也會在目標應用程式域中載入元件。When the CreateInstanceFrom method is used to create an instance in a target application domain, other than the application domain from which the call is made, the assembly is loaded in the target application domain. 但是,如果實例在呼叫的應用程式域中解除包裝,以特定方式使用未包裝的實例可能會導致元件載入至呼叫的應用程式域。However, if the instance is unwrapped in the calling application domain, using the unwrapped instance in certain ways can cause the assembly to be loaded into the calling application domain. 例如,在實例解除包裝之後,可能會要求其型別資訊,以便在晚期繫結時呼叫其方法。For example, after the instance is unwrapped, its type information might be requested, in order to call its methods late-bound. 將元件載入至呼叫的應用程式域時,可能會發生例外狀況。When the assembly is loaded into the calling application domain, exceptions can occur.

  • 如果先前已將相同元件的另一個版本載入至呼叫的應用程式域,或呼叫應用程式域的載入路徑與目標應用程式域的載入路徑不同,則 MissingMethodException 可能會發生例外狀況。If another version of the same assembly was previously loaded into the calling application domain, or if the load path of the calling application domain is different from that of the target application domain, exceptions such as MissingMethodException can occur.

  • 如果呼叫的應用程式域對實例型別進行早期繫結的呼叫, InvalidCastException 就可以在嘗試轉換實例時擲回。If the calling application domain makes early-bound calls to the instance type, InvalidCastException can be thrown when an attempt is made to cast the instance.

另請參閱

適用於

CreateInstanceFrom(String, String)

建立指定組件檔案中所定義之指定類型的新執行個體。Creates a new instance of the specified type defined in the specified assembly file.

public:
 System::Runtime::Remoting::ObjectHandle ^ CreateInstanceFrom(System::String ^ assemblyFile, System::String ^ typeName);
public:
 virtual System::Runtime::Remoting::ObjectHandle ^ CreateInstanceFrom(System::String ^ assemblyFile, System::String ^ typeName);
public System.Runtime.Remoting.ObjectHandle? CreateInstanceFrom (string assemblyFile, string typeName);
public System.Runtime.Remoting.ObjectHandle CreateInstanceFrom (string assemblyFile, string typeName);
member this.CreateInstanceFrom : string * string -> System.Runtime.Remoting.ObjectHandle
abstract member CreateInstanceFrom : string * string -> System.Runtime.Remoting.ObjectHandle
override this.CreateInstanceFrom : string * string -> System.Runtime.Remoting.ObjectHandle
Public Function CreateInstanceFrom (assemblyFile As String, typeName As String) As ObjectHandle

參數

assemblyFile
String

檔案的名稱 (包括路徑),此檔案包含會定義要求之類型的組件。The name, including the path, of a file that contains an assembly that defines the requested type. 此組件使用 LoadFrom(String) 方法載入。The assembly is loaded using the LoadFrom(String) method.

typeName
String

FullName 屬性傳回的要求類型之完整名稱 (包括命名空間,但不包括組件)。The fully qualified name of the requested type, including the namespace but not the assembly, as returned by the FullName property.

傳回

ObjectHandle

對新執行個體而言是包裝函式的物件,但如果沒有找到 typeName,則為 nullAn object that is a wrapper for the new instance, or null if typeName is not found. 傳回值需要解除包裝以存取實際物件。The return value needs to be unwrapped to access the real object.

實作

例外狀況

assemblyFilenullassemblyFile is null.

-或--or- typeNamenulltypeName is null.

找不到 assemblyFileassemblyFile was not found.

assemblyFile 中找不到 typeNametypeName was not found in assemblyFile.

嘗試對卸載的應用程式定義域執行作業。The operation is attempted on an unloaded application domain.

找不到無參數的公用建構函式。No parameterless public constructor was found.

呼叫端沒有足以呼叫這個建構函式的權限。The caller does not have sufficient permission to call this constructor.

assemblyFile 不是有效的組件。assemblyFile is not a valid assembly.

-或--or- 目前載入的是 Common Language Runtime 2.0 版或更新版本,編譯 assemblyFile 的版本還要更新。Version 2.0 or later of the common language runtime is currently loaded and assemblyFile was compiled with a later version.

使用兩個不同的辨識項載入組件或模組兩次。An assembly or module was loaded twice with two different evidences.

這個執行個體是 nullThis instance is null.

範例

下列範例顯示如何使用方法多載, CreateInstanceFrom(String, String) 在目標應用程式域中建立物件的實例,並呼叫其方法。The following example shows how to use the CreateInstanceFrom(String, String) method overload to create an instance of an object in a target application domain and call its methods.

此範例會定義 MarshalableExample 可以跨應用程式域界限封送處理的類別。The example defines the MarshalableExample class, which can be marshaled across application domain boundaries. 此範例會建立目前執行中元件的路徑、建立目標應用程式域,並使用 CreateInstanceFrom(String, String) 方法多載將範例元件載入至目標應用程式域,並建立的實例 MarshalableExampleThe example builds a path to the currently executing assembly, creates a target application domain, and uses the CreateInstanceFrom(String, String) method overload to load the example assembly into the target application domain and create an instance of MarshalableExample.

注意

在此範例中,路徑是絕對的,但相對路徑也可以運作,因為 Assembly.LoadFrom 方法是用來載入元件。The path is absolute in this example, but a relative path would also work because the Assembly.LoadFrom method is used to load the assembly.

解除包裝物件控制碼之後,此範例會示範三種在目標應用程式域中使用物件的方式:After unwrapping the object handle, the example demonstrates three ways to use an object in a target application domain:

  • 使用反映叫用具有晚期繫結的方法。Invoking a method with late binding, using reflection. 這需要類型資訊,這會導致元件載入至呼叫端的應用程式域中。This requires type information, which causes the assembly to be loaded into the application domain of the caller. (在此範例中,已載入。 ) (In this example, it is already loaded.)

  • 將物件轉換成呼叫端和被呼叫端都知道的介面。Casting the object to an interface known to both the caller and the callee. 如果介面是在呼叫元件中定義,或在呼叫端和被呼叫端所參考的第三個元件中定義,則呼叫的元件不會載入至呼叫端的應用程式域中。If the interface is defined in the calling assembly or in a third assembly referenced by both the caller and the callee, the called assembly is not loaded into the application domain of the caller.

  • 當呼叫端知道物件的型別時,直接使用該物件。Using the object directly when its type is known to the caller. 元件必須載入至呼叫端的應用程式域中。The assembly must be loaded into the application domain of the caller.

避免將被呼叫的元件載入呼叫端的應用程式域中的另一種方法,是讓呼叫端衍生自 MarshalByRefObject 類別,並定義可在目標應用程式域中執行的方法。Another way to avoid loading the called assembly into the application domain of the caller is for the caller to derive from the MarshalByRefObject class and to define a method that can be run in the target application domain. 該方法可以使用反映來檢查目標群組件,因為目標群組件已經載入到目標應用程式域中。That method can use reflection to examine a target assembly, because the target assembly is already loaded into the target application domain. 請參閱屬性的範例 DynamicDirectorySee the example for the DynamicDirectory property.

using namespace System;

public interface class ITest
{
    void Test(String^ greeting);
};

public ref class MarshalableExample : MarshalByRefObject, ITest
{
public:
    virtual void Test(String^ greeting)
    {
        Console::WriteLine("{0} from '{1}'!", greeting,
            AppDomain::CurrentDomain->FriendlyName);
    }
};

void main()
{
    // Construct a path to the current assembly.
    String^ assemblyPath = Environment::CurrentDirectory + "\\" +
        MarshalableExample::typeid->Assembly->GetName()->Name + ".exe";

    AppDomain^ ad = AppDomain::CreateDomain("MyDomain");
 
    System::Runtime::Remoting::ObjectHandle^ oh = 
        ad->CreateInstanceFrom(assemblyPath, "MarshalableExample");

    Object^ obj = oh->Unwrap();


    // Three ways to use the newly created object, depending on how
    // much is known about the type: Late bound, early bound through 
    // a mutually known interface, or early binding of a known type.
    //
    obj->GetType()->InvokeMember("Test", 
        System::Reflection::BindingFlags::InvokeMethod, 
        Type::DefaultBinder, obj, gcnew array<Object^> { "Hello" });

    ITest^ it = (ITest^) obj;
    it->Test("Hi");

    MarshalableExample^ ex = (MarshalableExample^) obj;
    ex->Test("Goodbye");
}

/* This example produces the following output:

Hello from 'MyDomain'!
Hi from 'MyDomain'!
Goodbye from 'MyDomain'!
 */
using System;

public interface ITest
{
    void Test(string greeting);
}

public class MarshalableExample : MarshalByRefObject, ITest
{
    static void Main()
    {
        // Construct a path to the current assembly.
        string assemblyPath = Environment.CurrentDirectory + "\\" +
            typeof(MarshalableExample).Assembly.GetName().Name + ".exe";

        AppDomain ad = AppDomain.CreateDomain("MyDomain");

        System.Runtime.Remoting.ObjectHandle oh =
            ad.CreateInstanceFrom(assemblyPath, "MarshalableExample");

        object obj = oh.Unwrap();

        // Three ways to use the newly created object, depending on how
        // much is known about the type: Late bound, early bound through
        // a mutually known interface, or early binding of a known type.
        //
        obj.GetType().InvokeMember("Test",
            System.Reflection.BindingFlags.InvokeMethod,
            Type.DefaultBinder, obj, new object[] { "Hello" });

        ITest it = (ITest) obj;
        it.Test("Hi");

        MarshalableExample ex = (MarshalableExample) obj;
        ex.Test("Goodbye");
    }

    public void Test(string greeting)
    {
        Console.WriteLine("{0} from '{1}'!", greeting,
            AppDomain.CurrentDomain.FriendlyName);
    }
}

/* This example produces the following output:

Hello from 'MyDomain'!
Hi from 'MyDomain'!
Goodbye from 'MyDomain'!
 */
Public Interface ITest

    Sub Test(ByVal greeting As String)
End Interface

Public Class MarshalableExample 
    Inherits MarshalByRefObject
    Implements ITest

    Shared Sub Main()
    
        ' Construct a path to the current assembly.
        Dim assemblyPath As String = Environment.CurrentDirectory & "\" &
            GetType(MarshalableExample).Assembly.GetName().Name & ".exe"

        Dim ad As AppDomain = AppDomain.CreateDomain("MyDomain")
 
        Dim oh As System.Runtime.Remoting.ObjectHandle = 
            ad.CreateInstanceFrom(assemblyPath, "MarshalableExample")

        Dim obj As Object = oh.Unwrap()


        ' Three ways to use the newly created object, depending on how
        ' much is known about the type: Late bound, early bound through 
        ' a mutually known interface, or early binding of a known type.
        '
        obj.GetType().InvokeMember("Test", 
            System.Reflection.BindingFlags.InvokeMethod, 
            Type.DefaultBinder, obj, New Object() { "Hello" })

        Dim it As ITest = CType(obj, ITest) 
        it.Test("Hi")

        Dim ex As MarshalableExample = CType(obj, MarshalableExample) 
        ex.Test("Goodbye")
    End Sub

    Public Sub Test(ByVal greeting As String) Implements ITest.Test
    
        Console.WriteLine("{0} from '{1}'!", greeting,
            AppDomain.CurrentDomain.FriendlyName)
    End Sub
End Class

' This example produces the following output:
'
'Hello from 'MyDomain'!
'Hi from 'MyDomain'!
'Goodbye from 'MyDomain'!

備註

叫用的無參數函數 typeNameThe parameterless constructor for typeName is invoked.

如需詳細資訊,請參閱 Activator.CreateInstanceFrom 方法。For more information, see the Activator.CreateInstanceFrom method.

CreateInstanceFrom 方法用來在目標應用程式域中建立實例時,除了進行呼叫的應用程式域以外,也會在目標應用程式域中載入元件。When the CreateInstanceFrom method is used to create an instance in a target application domain, other than the application domain from which the call is made, the assembly is loaded in the target application domain. 但是,如果實例在呼叫的應用程式域中解除包裝,以特定方式使用未包裝的實例可能會導致元件載入至呼叫的應用程式域。However, if the instance is unwrapped in the calling application domain, using the unwrapped instance in certain ways can cause the assembly to be loaded into the calling application domain. 例如,在實例解除包裝之後,可能會要求其型別資訊,以便在晚期繫結時呼叫其方法。For example, after the instance is unwrapped, its type information might be requested, in order to call its methods late-bound. 將元件載入至呼叫的應用程式域時,可能會發生例外狀況。When the assembly is loaded into the calling application domain, exceptions can occur.

  • 如果先前已將相同元件的另一個版本載入至呼叫的應用程式域,或呼叫應用程式域的載入路徑與目標應用程式域的載入路徑不同,則 MissingMethodException 可能會發生例外狀況。If another version of the same assembly was previously loaded into the calling application domain, or if the load path of the calling application domain is different from that of the target application domain, exceptions such as MissingMethodException can occur.

  • 如果呼叫的應用程式域對實例型別進行早期繫結的呼叫, InvalidCastException 就可以在嘗試轉換實例時擲回。If the calling application domain makes early-bound calls to the instance type, InvalidCastException can be thrown when an attempt is made to cast the instance.

另請參閱

適用於