Share via


在另一个应用程序域中执行代码(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 编程指南

应用程序域

使用应用程序域和程序集编程