AppDomain.CreateInstanceFrom 方法

定義

建立指定組件檔案中所定義之指定類型的新執行個體。

多載

CreateInstanceFrom(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
已淘汰.

建立指定組件檔案中所定義之指定類型的新執行個體。

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

建立指定組件檔案中所定義之指定類型的新執行個體。

CreateInstanceFrom(String, String, Object[])

建立指定組件檔案中所定義之指定類型的新執行個體。

CreateInstanceFrom(String, String)

建立指定組件檔案中所定義之指定類型的新執行個體。

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.

建立指定組件檔案中所定義之指定類型的新執行個體。

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 (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);
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
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

檔案的名稱 (包括路徑),此檔案包含會定義要求之類型的組件。 此組件使用 LoadFrom(String) 方法載入。

typeName
String

FullName 屬性傳回的要求類型之完整名稱 (包括命名空間,但不包括組件)。

ignoreCase
Boolean

布林值,指出是否執行區分大小寫的搜尋。

bindingAttr
BindingFlags

零或多個位元旗標的組合,此位元旗標會影響 typeName 建構函式的搜尋。 如果 bindingAttr 為零,則會針對公用建構函式執行區分大小寫的搜尋。

binder
Binder

使用反映來啟用繫結、強制引數的類型、成員的引動過程,和擷取 MemberInfo 物件的物件。 如果 binder 為 null,則會使用預設繫結器。

args
Object[]

要傳遞到建構函式的引數。 這個引數陣列必須在數目、順序和類型上符合要叫用之建構函式的參數。 如果慣用無參數建構函式,則 args 必須是空陣列或 Null。

culture
CultureInfo

特定文化特性的資訊,其可控制 args 到型式類型 (為 typeName 建構函式宣告) 的強制轉型。 如果 culturenull,會使用目前執行緒的 CultureInfo

activationAttributes
Object[]

一或多個屬性的陣列,此屬性可參與啟動過程。 陣列通常只會包含一個 UrlAttribute 物件來指定用以啟動遠端物件的 URL。

此參數與啟動了用戶端的物件相關。 用戶端啟動是一項舊的技術,保留目的在提供回溯相容性,不建議用於新的開發。 分散式應用程式應該改用 Windows Communication Foundation。

securityAttributes
Evidence

用來授權建立 typeName 的資訊。

傳回

對新執行個體而言是包裝函式的物件,但如果沒有找到 typeName,則為 null。 傳回值需要解除包裝以存取實際物件。

實作

屬性

例外狀況

assemblyFilenull

-或-

typeNamenull

呼叫者無法提供非繼承自 MarshalByRefObject 之物件的啟動屬性。

-或-

securityAttributes 不是 null。 若未啟用舊版 CAS 原則,securityAttributes 應為 null

嘗試對卸載的應用程式定義域執行作業。

找不到 assemblyFile

assemblyFile 中找不到 typeName

找不到相符的公用建構函式。

呼叫端沒有足以呼叫這個建構函式的權限。

assemblyFile 不是目前載入運行時間的有效元件。

使用兩個不同的辨識項載入組件或模組兩次。

這個執行個體是 null

備註

如需這個方法的詳細資訊,請參閱 Activator.CreateInstanceFrom 方法。

CreateInstanceFrom當方法用來在目標應用程式域中建立實例時,除了呼叫的來源應用程式域之外,元件會載入目標應用程式域。 不過,如果實例在呼叫的應用程式域中解除包裝,以特定方式使用未包裝的實例,可能會導致元件載入呼叫應用程式域。 例如,在取消包裝實例之後,可能會要求其類型資訊,以呼叫其方法晚期系結。 當元件載入至呼叫的應用程式域時,可能會發生例外狀況。

  • 如果先前已將另一個相同元件的版本載入至呼叫應用程式域,或呼叫應用程式域的載入路徑與目標應用程式域的載入路徑不同,則可能發生例外狀況,例如 MissingMethodException

  • 如果呼叫應用程式域對實例類型進行早期系結呼叫, InvalidCastException 可以在嘗試轉換實例時擲回。

另請參閱

適用於

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

來源:
AppDomain.cs
來源:
AppDomain.cs
來源:
AppDomain.cs

建立指定組件檔案中所定義之指定類型的新執行個體。

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

檔案的名稱 (包括路徑),此檔案包含會定義要求之類型的組件。 此組件使用 LoadFrom(String) 方法載入。

typeName
String

FullName 屬性傳回的要求類型之完整名稱 (包括命名空間,但不包括組件)。

ignoreCase
Boolean

布林值,指出是否執行區分大小寫的搜尋。

bindingAttr
BindingFlags

零或多個位元旗標的組合,此位元旗標會影響 typeName 建構函式的搜尋。 如果 bindingAttr 為零,則會針對公用建構函式執行區分大小寫的搜尋。

binder
Binder

使用反映來啟用繫結、強制引數的類型、成員的引動過程,和擷取 MemberInfo 物件的物件。 如果 binder 為 null,則會使用預設繫結器。

args
Object[]

要傳遞到建構函式的引數。 這個引數陣列必須在數目、順序和類型上符合要叫用之建構函式的參數。 如果慣用無參數建構函式,則 args 必須是空陣列或 Null。

culture
CultureInfo

特定文化特性的資訊,其可控制 args 到型式類型 (為 typeName 建構函式宣告) 的強制轉型。 如果 culturenull,會使用目前執行緒的 CultureInfo

activationAttributes
Object[]

一或多個屬性的陣列,此屬性可參與啟動過程。 陣列通常只會包含一個 UrlAttribute 物件來指定用以啟動遠端物件的 URL。

此參數與啟動了用戶端的物件相關。 用戶端啟動是一項舊的技術,保留目的在提供回溯相容性,不建議用於新的開發。 分散式應用程式應該改用 Windows Communication Foundation。

傳回

對新執行個體而言是包裝函式的物件,但如果沒有找到 typeName,則為 null。 傳回值需要解除包裝以存取實際物件。

例外狀況

assemblyFilenull

-或-

typeNamenull

呼叫者無法提供非繼承自 MarshalByRefObject 之物件的啟動屬性。

嘗試對卸載的應用程式定義域執行作業。

找不到 assemblyFile

assemblyFile 中找不到 typeName

找不到相符的公用建構函式。

呼叫端沒有足以呼叫這個建構函式的權限。

assemblyFile 不是目前載入運行時間的有效元件。

使用兩個不同的辨識項載入組件或模組兩次。

這個執行個體是 null

備註

如需詳細資訊,請參閱 Activator.CreateInstanceFrom 方法。

CreateInstanceFrom當方法用來在目標應用程式域中建立實例時,除了呼叫的來源應用程式域之外,元件會載入目標應用程式域。 不過,如果實例在呼叫的應用程式域中解除包裝,以特定方式使用未包裝的實例,可能會導致元件載入呼叫應用程式域。 例如,在取消包裝實例之後,可能會要求其類型資訊,以呼叫其方法晚期系結。 當元件載入至呼叫的應用程式域時,可能會發生例外狀況。

  • 如果先前已將另一個相同元件的版本載入至呼叫應用程式域,或呼叫應用程式域的載入路徑與目標應用程式域的載入路徑不同,則可能發生例外狀況,例如 MissingMethodException

  • 如果呼叫應用程式域對實例類型進行早期系結呼叫, InvalidCastException 可以在嘗試轉換實例時擲回。

另請參閱

適用於

CreateInstanceFrom(String, String, Object[])

來源:
AppDomain.cs
來源:
AppDomain.cs
來源:
AppDomain.cs

建立指定組件檔案中所定義之指定類型的新執行個體。

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

檔案的名稱 (包括路徑),此檔案包含會定義要求之類型的組件。 此組件使用 LoadFrom(String) 方法載入。

typeName
String

FullName 屬性傳回的要求類型之完整名稱 (包括命名空間,但不包括組件)。

activationAttributes
Object[]

一或多個屬性的陣列,此屬性可參與啟動過程。 陣列通常只會包含一個 UrlAttribute 物件來指定用以啟動遠端物件的 URL。

此參數與 client-activated 物件有關。用戶端啟動是一項舊的技術,保留目的在提供回溯相容性,不建議用於新的開發。 分散式應用程式應該改用 Windows Communication Foundation。

傳回

對新執行個體而言是包裝函式的物件,但如果沒有找到 typeName,則為 null。 傳回值需要解除包裝以存取實際物件。

實作

例外狀況

assemblyFilenull

找不到 assemblyFile

assemblyFile 中找不到 typeName

呼叫端沒有足以呼叫這個建構函式的權限。

找不到相符的公用建構函式。

呼叫者無法提供非繼承自 MarshalByRefObject 之物件的啟動屬性。

嘗試對卸載的應用程式定義域執行作業。

assemblyFile 不是目前載入運行時間的有效元件。

使用兩個不同的辨識項載入組件或模組兩次。

這個執行個體是 null

備註

會叫用 的 typeName 無參數建構函式。

如需這個方法的詳細資訊,請參閱 Activator.CreateInstanceFrom 方法。

CreateInstanceFrom使用 方法在目標應用程式域中建立實例時,除了呼叫的來源應用程式域之外,元件會載入目標應用程式域中。 不過,如果實例在呼叫的應用程式域中解除包裝,使用未包裝的實例在某些情況下,可能會導致元件載入呼叫應用程式域。 例如,在解除包裝實例之後,可能會要求其類型資訊,以便呼叫其方法晚期綁定。 當元件載入呼叫的應用程式域時,可能會發生例外狀況。

  • 如果先前已將相同元件的另一個版本載入至呼叫的應用程式域,或呼叫應用程式域的載入路徑與目標應用程式域的載入路徑不同,則可能發生例外 MissingMethodException 狀況。

  • 如果呼叫應用程式域對實例類型進行早期系結呼叫, InvalidCastException 可以在嘗試轉換實例時擲回。

另請參閱

適用於

CreateInstanceFrom(String, String)

來源:
AppDomain.cs
來源:
AppDomain.cs
來源:
AppDomain.cs

建立指定組件檔案中所定義之指定類型的新執行個體。

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

檔案的名稱 (包括路徑),此檔案包含會定義要求之類型的組件。 此組件使用 LoadFrom(String) 方法載入。

typeName
String

FullName 屬性傳回的要求類型之完整名稱 (包括命名空間,但不包括組件)。

傳回

對新執行個體而言是包裝函式的物件,但如果沒有找到 typeName,則為 null。 傳回值需要解除包裝以存取實際物件。

實作

例外狀況

assemblyFilenull

-或-

typeNamenull

找不到 assemblyFile

assemblyFile 中找不到 typeName

嘗試對卸載的應用程式定義域執行作業。

找不到無參數的公用建構函式。

呼叫端沒有足以呼叫這個建構函式的權限。

assemblyFile 不是目前載入運行時間的有效元件。

使用兩個不同的辨識項載入組件或模組兩次。

這個執行個體是 null

範例

下列範例示範如何使用 CreateInstanceFrom(String, String) 方法多載,在目標應用程式域中建立 對象的實例,並呼叫其方法。

此範例會 MarshalableExample 定義 類別,此類別可以跨應用程式域界限封送處理。 此範例會建置目前執行之元件的路徑、建立目標應用程式域,並使用 CreateInstanceFrom(String, String) 方法多載將範例元件載入目標應用程式域,並建立的 MarshalableExample實例。

注意

此範例中的路徑是絕對路徑,但相對路徑也會運作,因為 Assembly.LoadFrom 方法是用來載入元件。

解除包裝物件句柄之後,此範例會示範在目標應用程式域中使用物件的三種方式:

  • 使用反映叫用具有晚期綁定的方法。 這需要類型資訊,這會導致元件載入呼叫端的應用程式域。 (在此範例中,它已經載入。)

  • 將物件轉換成呼叫端和被呼叫端已知的介面。 如果在呼叫端和被呼叫端所參考的第三個元件中定義介面,則呼叫的元件不會載入呼叫端的應用程式域。

  • 當呼叫端知道其型別時,直接使用 物件。 元件必須載入呼叫端的應用程式域。

另一個避免將呼叫端元件載入呼叫端應用程式域的方法,是讓呼叫端衍生自 MarshalByRefObject 類別,並定義可在目標應用程式域中執行的方法。 該方法可以使用反映來檢查目標元件,因為目標元件已經載入目標應用程式域。 請參閱 屬性的 DynamicDirectory 範例。

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'!
 */
open System

type ITest =
    abstract Test: string -> unit

type MarshalableExample() =
    inherit MarshalByRefObject()
    
    member _.Test greeting =
        printfn $"{greeting} from '{AppDomain.CurrentDomain.FriendlyName}'!"

    interface ITest with
        member this.Test message = this.Test message 

// Construct a path to the current assembly.
let assemblyPath = 
    Environment.CurrentDirectory + "\\" + typeof<MarshalableExample>.Assembly.GetName().Name + ".exe"

let ad = AppDomain.CreateDomain "MyDomain"

let oh =
    ad.CreateInstanceFrom(assemblyPath, "MarshalableExample")

let 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, [| box "Hello" |])
|> ignore

let it = obj :?> ITest
it.Test "Hi"

let ex = obj :?> MarshalableExample
ex.Test("Goodbye")

(* 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'!

備註

會叫用 的 typeName 無參數建構函式。

如需詳細資訊,請參閱 Activator.CreateInstanceFrom 方法。

CreateInstanceFrom使用 方法在目標應用程式域中建立實例時,除了呼叫的來源應用程式域之外,元件會載入目標應用程式域中。 不過,如果實例在呼叫的應用程式域中解除包裝,使用未包裝的實例在某些情況下,可能會導致元件載入呼叫應用程式域。 例如,在解除包裝實例之後,可能會要求其類型資訊,以便呼叫其方法晚期綁定。 當元件載入呼叫的應用程式域時,可能會發生例外狀況。

  • 如果先前已將相同元件的另一個版本載入至呼叫的應用程式域,或呼叫應用程式域的載入路徑與目標應用程式域的載入路徑不同,則可能發生例外 MissingMethodException 狀況。

  • 如果呼叫應用程式域對實例類型進行早期系結呼叫, InvalidCastException 可以在嘗試轉換實例時擲回。

另請參閱

適用於