简体   繁体   English

DoCallBack非静态委托的CrossAppDomainDelegate行为

[英]DoCallBack CrossAppDomainDelegate behavior for non-static delegates

Please consider the following piece of code: 请考虑以下代码:

// Create a new application domain
AppDomain ad = AppDomain.CreateDomain("New domain");

Worker work = new Worker();

// if Worker class is marked as 'MarshalByRefObject', this will run in current
// appdomain.
// if Worker class is NOT marked as 'MarshalByRefObject' and is marked as
// 'Serializable', this will run in a new appdomain.
ad.DoCallBack(work.PrintDomain);
// or ad.DoCallBack(new CrossAppDomainDelegate(work.PrintDomain));

// But for static methods:
// If ppp method is static, no marking is required and it will run in
// a new AppDomain.
ad.DoCallBack(Worker.ppp);

How do we explain this behavior of DoCallBack ? 我们如何解释DoCallBack这种行为?

  1. Why is the non-static method PrintDomain executed in the current domain when the Worker class is marked MarshalByRefObject ? Worker类标记为MarshalByRefObject时,为什么在当前域中执行非静态方法PrintDomain
  2. Why is the non-static method PrintDomain executed in a new AppDomain when the Worker class is marked Serializable ? Worker类标记为Serializable时,为什么在新的AppDomain中执行非静态方法PrintDomain
  3. Why doesn't the static method need any markings? 为什么静态方法不需要任何标记?

Why is the non-static method PrintDomain executed in the current domain when the Worker class is marked MarshalByRefObject? 当Worker类标记为MarshalByRefObject时,为什么在当前域中执行非静态方法PrintDomain?

Because that's what MBRO does, it creates a proxy for the object that you created in your primary appdomain. 因为这是MBRO所做的,它会为您在主应用程序域中创建的对象创建代理。 Which marshals the call from the secondary appdomain to the appdomain that owns the object, the primary appdomain. 其中将来自辅助应用程序域的调用编组到拥有该对象(主应用程序域)的应用程序域。

Why is the non-static method PrintDomain executed in a new AppDomain when the Worker class is marked Serializable? 当Worker类标记为Serializable时,为什么在新的AppDomain中执行非静态方法PrintDomain?

Because that scenario does not use a proxy. 因为该方案使用代理服务器。 The object itself is marshaled from the primary to the secondary appdomain. 对象本身从主应用程序域到辅助应用程序域进行编组。 Possible because you marked it [Serializable]. 可能是因为您将其标记为[可序列化]。 The call therefore executes in the secondary appdomain. 因此,调用在辅助appdomain中执行。

Why doesn't the static method need any markings? 为什么静态方法不需要任何标记?

It is unclear what you mean by "markings", but it isn't any different for a static method. 目前还不清楚“标记”是什么意思,但静态方法没有任何不同。 Some code to play with, remove the comment on the base class to compare the two scenarios: 一些代码可以使用,删除基类上的注释来比较两个场景:

using System;

class Program {
    static void Main(string[] args) {
        var dom = AppDomain.CreateDomain("Test");
        var obj = new WorkerMbro();
        dom.DoCallBack(obj.PrintDomain);
        dom.DoCallBack(obj.PrintDomainStatic);
        Console.ReadLine();
    }
}
[Serializable]
class WorkerMbro /* : MarshalByRefObject */ {
    public void PrintDomain() {
        Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
    }
    public void PrintDomainStatic() {
        Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
    }
}

Output as posted: 发布的输出:

Test
Test

Output with the comments removed so the proxy is used: 输出删除了注释,以便使用代理:

ConsoleApplication1.vshost.exe
ConsoleApplication1.vshost.exe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何调用非静态方法 - How to call a non-static method 将非静态代码与静态main方法一起使用 - Using non-static code with static main method OOP(C#)中的静态和非静态类的入门难题 - A Beginner Puzzle of Static and Non-Static Classes in OOP ( C# ) 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property ERROR静态方法需要null实例,非静态方法需要非null实例 - ERROR Static method requires null instance, non-static method requires non-null instance 非静态字段,方法或属性“ Microsoft.Kinect.KinectSensor.Start()”需要对象引用。 - An object reference is required for the non-static field, method, or property 'Microsoft.Kinect.KinectSensor.Start()' 非静态字段,方法或属性'_Default.RadioButtonList2'需要对象引用 - An object reference is required for the non-static field, method, or property '_Default.RadioButtonList2' 我应该将“自定义单元测试声明”类设为非静态吗? - Should I make my Custom Unit Test Assert Class Non-Static? 帮助错误C#:非静态字段,方法或属性需要对象引用RpgTutorial.Character.Swordsmanship - Help With Error C#: An object reference is required for the non-static field, method, or property RpgTutorial.Character.Swordsmanship MOQ - 如何验证静态类调用和委托? - MOQ - How to verify static class call and delegates?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM