System.AppDomain.DoCallBack 方法

方法描述

在另一个应用程序域中执行代码,该应用程序域由指定的委托标识。

语法定义(C# System.AppDomain.DoCallBack 方法 的用法)

public void DoCallBack(
	CrossAppDomainDelegate callBackDelegate
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
callBackDelegate System-CrossAppDomainDelegate 指定要调用的方法的委托。
返回值 void

提示和注释

callBackDelegate 可指定按值封送、MarshalByRefObject 或 ContextBoundObject。

System.AppDomain.DoCallBack 方法例子

下面的示例演示如何通过引用使用 DoCallBack 方法。

public class PingPong : MarshalByRefObject
{
    private string greetings = "PING!";

    public static void Main()
    {
        AppDomain otherDomain = AppDomain.CreateDomain("otherDomain");

        PingPong pp = new PingPong();
        pp.MyCallBack();
        pp.greetings = "PONG!";
        otherDomain.DoCallBack(new CrossAppDomainDelegate(pp.MyCallBack));

        // Output:
        //   PING! from defaultDomain
        //   PONG! from defaultDomain
    }

    // Callback will always execute within defaultDomain due to inheritance from
    // MarshalByRefObject
    public void MyCallBack()
    {
        string name = AppDomain.CurrentDomain.FriendlyName;
        if (name == AppDomain.CurrentDomain.SetupInformation.ApplicationName)
        {
            name = "defaultDomain";
        }
        Console.WriteLine(greetings + " from " + name);
    }
}

异常

异常 异常描述
ArgumentNullException callBackDelegate 为 null。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。