.NET Remoting 进阶之四
其他上下文中访问 Context Agile 对象
ContextAgile 对象
该例中,上下文中执行的 Main() 方法调用了 MyAgileClass 对象的 DisplayContextInfo 方法。还将一个 MyAgileClass 类型的对象 myAgile 传递给 MyContextBoundClass 的构造函数。在上下文1中执行的构造函数,先执行默认构造后,调用 myAgile 的 DisplayContextInfo 方法。通过输出结果可以发现:MyAgileClass 类型的对象可以在上下文间自由地移动。即:上下文 id 分别为 0 和 1。 using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Contexts; using System.Threading; namespace ContextTest { /// <summary> /// MyContextBoundClass 的摘要说明。 /// </summary> [Synchronization] public class MyContextBoundClass: ContextBoundObject { public MyContextBoundClass() { Console.WriteLine("in MyContextBoundClass Constructor"); Diagnostics.DisplayContextInfo(); //static method by className } //:this() means call the default constructor before execute other codes public MyContextBoundClass(MyAgileClass myAgile):this() { myAgile.DisplayContextInfo();//myAgile should be in the context bound's context for it is an Agile object } } public class MyAgileClass { public void DisplayContextInfo() { Console.WriteLine("MyAgileClass.DisplayContextInfo()"); Diagnostics.DisplayContextInfo(); } } public class Diagnostics { public static void DisplayContextInfo() { Context ctx = Thread.CurrentContext; Console.WriteLine("Properties for context id:{0}", ctx.ContextID); foreach(IContextProperty ctxProp in ctx.ContextProperties) { Console.WriteLine("{0}",ctxProp.Name); } } } class ContextTest { static void Main(string[] args) { MyAgileClass myAgile = new MyAgileClass(); 本文出自 51CTO.COM技术博客myAgile.DisplayContextInfo(); MyContextBoundClass myBound = new MyContextBoundClass(myAgile); Console.ReadLine(); } } } |



august
博客统计信息
热门文章
最新评论
友情链接
