共用方式為


在其他應用程式定義域中執行程式碼 (C# 和 Visual Basic)

組件一載入到應用程式定義域後,即可執行組件所包含的程式碼。 若要載入組件,最簡單的方式是使用 AssemblyLoad,這會將組件載入到目前的應用程式定義域,並從組件的預設進入點開始執行程式碼。

如果您想將組件載入至另一個應用程式定義域,請使用 ExecuteAssemblyExecuteAssemblyByName,或這些方法之其他多載版本中的其中一個。

如果您要執行的組件是從預設進入點以外的點啟動,請在遠端組件中定義衍生自 MarshalByRefObject 的新型別。 然後再使用 CreateInstance 在應用程式中建立這個型別的執行個體。

下列程式碼建立包含單一命名空間和兩個類別組成的組件。 將程式碼貼到名為HelloWorldRemote 的 Visual Studio 主控台應用程式中、組建或執行方案,然後關閉方案。 在您的專案的 obj/Debug 資料夾中尋找 HelloWorldRemote.exe 檔案,並且將該檔案複製到 C: 磁碟機。

' This module contains code to be called.
Module HelloWorldRemote
    Class RemoteObject
        Inherits System.MarshalByRefObject
        Sub RemoteObject()
            System.Console.WriteLine("Hello, World! (RemoteObject Constructor)")
        End Sub
    End Class
    Sub Main()
        System.Console.WriteLine("Hello, World! (Main method)")
    End Sub
End Module
// This namespace contains code to be called.
namespace HelloWorldRemote
{
    public class RemoteObject : System.MarshalByRefObject
    {
        public RemoteObject()
        {
            System.Console.WriteLine("Hello, World! (RemoteObject Constructor)");
        }
    }
    class Program
    {
        static void Main()
        {
            System.Console.WriteLine("Hello, World! (Main method)");
        }
    }
}

若要從另一個應用程式存取程式碼,您可以將組件載入到目前的應用程式定義域,或建立新的應用程式定義域,再將組件載入到這個新應用程式定義域中。

若要使用 Assembly.LoadFrom 將組件載入至目前的應用程式定義域,您可以使用 Assembly.CreateInstance 來具現化 RemoteObject 類別的執行個體。 具現化會導致執行物件建構函式。

' Load the assembly into the current appdomain:
Dim newAssembly As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom("c:\HelloWorldRemote.exe")

' Instantiate RemoteObject:
newAssembly.CreateInstance("HelloWorldRemote.RemoteObject")
// Load the assembly into the current appdomain:
System.Reflection.Assembly newAssembly = System.Reflection.Assembly.LoadFrom(@"c:\HelloWorldRemote.exe");

// Instantiate RemoteObject:
newAssembly.CreateInstance("HelloWorldRemote.RemoteObject");

當組件載入到獨立的應用程式定義域時,可以使用 AppDomain.ExecuteAssembly 來存取預設進入點,或使用 AppDomain.CreateInstance 來建立 RemoteObject 類別的執行個體。 建立執行個體會讓建構函式開始執行。

注意事項注意事項

如需使用 Assembly.LoadFrom 之缺點的詳細資訊,請參閱 Assembly.LoadFrom(String) 的<備註>一節。

Dim NewAppDomain As System.AppDomain = System.AppDomain.CreateDomain("NewApplicationDomain")

' Load the assembly and call the default entry point:
NewAppDomain.ExecuteAssembly("c:\HelloWorldRemote.exe")

' Create an instance of RemoteObject:
NewAppDomain.CreateInstanceFrom("c:\HelloWorldRemote.exe", "HelloWorldRemote.RemoteObject")
System.AppDomain NewAppDomain = System.AppDomain.CreateDomain("NewApplicationDomain");

// Load the assembly and call the default entry point:
NewAppDomain.ExecuteAssembly(@"c:\HelloWorldRemote.exe");

// Create an instance of RemoteObject:
NewAppDomain.CreateInstanceFrom(@"c:\HelloWorldRemote.exe", "HelloWorldRemote.RemoteObject");

如果您不想以程式設計方式載入組件,請使用 [方案總管] 中的 [加入參考] 來指定組件 HelloWorldRemote.exe。 在 C# 中,請加入 using HelloWorldRemote; 指示詞;在 Visual Basic 中,請加入 Imports HelloWorldRemote 陳述式。 接著,請在您的程式中使用 RemoteObject 宣告 RemoteObject 物件的執行個體,如下列範例所示。

' This code creates an instance of RemoteObject, 
' assuming HelloWorldRemote has been added as a reference:
Dim o As HelloWorldRemote.RemoteObject = New HelloWorldRemote.RemoteObject()
// This code creates an instance of RemoteObject, 
// assuming HelloWorldRemote has been added as a reference:
HelloWorldRemote.RemoteObject o = new HelloWorldRemote.RemoteObject();

請參閱

參考

應用程式定義域 (C# 和 Visual Basic)

概念

C# 程式設計手冊

應用程式定義域和組件

使用應用程式定義域設計程式

其他資源

Visual Basic 程式設計手冊

應用程式定義域

使用應用程式定義域和組件設計程式