Robin's Blog

记录 积累 学习 成长

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. using System.Runtime.Remoting.Contexts;  
  7. using System.Runtime.Remoting.Activation;  
  8. using System.Runtime.Remoting.Messaging;  
  9. using System.Reflection;  
  10.   
  11. namespace ConsoleApplication1  
  12. {  
  13.   
  14.   
  15.     public class UserTrack : ContextAttribute  
  16.     {  
  17.         public UserTrack() : base("UserCreate") { Console.WriteLine("UserContextAttribute Create..."); }  
  18.         public override void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)  
  19.         {  
  20.             ctorMsg.ContextProperties.Add(new Processer());  
  21.             //base.GetPropertiesForNewContext(ctorMsg);  
  22.         }  
  23.     }  
  24.   
  25.     [UserTrack()]  
  26.     public class User : ContextBoundObject  
  27.     {  
  28.         private string _userName = "";  
  29.         private string _nick = "";  
  30.         public User() { Console.WriteLine("UserObject Create..."); }  
  31.         public string UserName  
  32.         {  
  33.             get { return this._userName; }  
  34.             set { this._userName = value; }  
  35.         }  
  36.   
  37.         public string Nick  
  38.         {  
  39.             get { return this._nick; }  
  40.             set { this._nick = value; }  
  41.         }  
  42.   
  43.         public string Password = "";  
  44.     }  
  45.   
  46.   
  47.     public class Processer : IContextProperty, IContributeObjectSink  
  48.     {  
  49.  
  50.         #region IContextProperty 成员  
  51.   
  52.         public void Freeze(Context newContext)  
  53.         {  
  54.   
  55.         }  
  56.   
  57.         public bool IsNewContextOK(Context newCtx)  
  58.         {  
  59.             return true;  
  60.         }  
  61.   
  62.         public string Name  
  63.         {  
  64.             get { return "mmcgzs.tracker"; }  
  65.         }  
  66.  
  67.         #endregion  
  68.   
  69.         #region IContributeObjectSink 成员  
  70.   
  71.         public IMessageSink GetObjectSink(MarshalByRefObject obj, System.Runtime.Remoting.Messaging.IMessageSink nextSink)  
  72.         {  
  73.             return new TraceSink(obj, nextSink);  
  74.         }  
  75.  
  76.         #endregion  
  77.     }  
  78.   
  79.     public class TraceSink : IMessageSink  
  80.     {  
  81.         private User _sender;  
  82.         private IMessageSink _nextSink;  
  83.   
  84.         public TraceSink(MarshalByRefObject sender, IMessageSink nextSink)  
  85.         {  
  86.             this._nextSink = nextSink;  
  87.             this._sender = sender as User;  
  88.         }  
  89.  
  90.  
  91.         #region IMessageSink 成员  
  92.   
  93.         public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)  
  94.         {  
  95.             return null;  
  96.         }  
  97.   
  98.         public IMessageSink NextSink  
  99.         {  
  100.             get { return this._nextSink; }  
  101.         }  
  102.   
  103.         public IMessage SyncProcessMessage(IMessage msg)  
  104.         {  
  105.             IMethodCallMessage call = msg as IMethodCallMessage;  
  106.   
  107.             PropertyAction property = GetCallProperty(call);  
  108.             if (property != null)  
  109.             {  
  110.                 Console.WriteLine(property.Action+" Property " + property.Name + " value " + property.Value);  
  111.             }  
  112.             return this._nextSink.SyncProcessMessage(call);  
  113.         }  
  114.   
  115.   
  116.         private PropertyAction GetCallProperty(IMethodCallMessage call)  
  117.         {  
  118.             PropertyAction p = new PropertyAction();  
  119.   
  120.             if (call == null)  
  121.                 return null;  
  122.   
  123.             // 得到调用的方法的信息  
  124.             MethodBase methodInfo = call.MethodBase;  
  125.   
  126.             if (methodInfo.Name.StartsWith("set_"))  
  127.             {  
  128.                 p.Name = methodInfo.Name.Substring(4);  
  129.                 p.Value = call.InArgs[0];  
  130.                 p.Action = methodInfo.Name.Substring(0, 3);  
  131.             }  
  132.             else if (methodInfo.Name.StartsWith("get_"))  
  133.             {  
  134.                 p.Name = methodInfo.Name.Substring(4);  
  135.                 p.Value = methodInfo.Invoke(this._sender, null);  
  136.                 p.Action = methodInfo.Name.Substring(0, 3);  
  137.             }  
  138.             else if (methodInfo.Name == "FieldSetter")  
  139.             {  
  140.                 p.Name = (string)call.InArgs[1];  
  141.                 p.Value = call.InArgs[2];  
  142.                 p.Action = methodInfo.Name;  
  143.             }  
  144.             else if (methodInfo.Name == "FieldGetter")  
  145.             {  
  146.                 p.Name = (string)call.InArgs[1];  
  147.                 p.Value = this._sender.GetType().GetField(p.Name).GetValue(this._sender);  
  148.                 p.Action = methodInfo.Name;  
  149.             }  
  150.             else  
  151.             {  
  152.                 p = null;  
  153.             }  
  154.   
  155.             return p;  
  156.         }  
  157.  
  158.         #endregion  
  159.     }  
  160.   
  161.     public class PropertyAction {  
  162.         public string Name = "";  
  163.         public object Value = null;  
  164.         public string Action = "";  
  165.     }  
  166.   
  167.     class Program  
  168.     {  
  169.         static void Main(string[] args)  
  170.         {  
  171.             User user = new User();  
  172.             user.UserName = "mmcgzs";  
  173.             user.Nick = "毛毛虫";  
  174.             user.Password = "888888";  
  175.             string a = user.UserName;  
  176.             a = user.Password;  
  177.             Console.Read();  
  178.         }  
  179.     }  
  180.   
  181. }  
posted on 2010-01-17 18:11  Robin99  阅读(482)  评论(0)    收藏  举报