- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.Remoting.Contexts;
- using System.Runtime.Remoting.Activation;
- using System.Runtime.Remoting.Messaging;
- using System.Reflection;
- namespace ConsoleApplication1
- {
- public class UserTrack : ContextAttribute
- {
- public UserTrack() : base("UserCreate") { Console.WriteLine("UserContextAttribute Create..."); }
- public override void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)
- {
- ctorMsg.ContextProperties.Add(new Processer());
- //base.GetPropertiesForNewContext(ctorMsg);
- }
- }
- [UserTrack()]
- public class User : ContextBoundObject
- {
- private string _userName = "";
- private string _nick = "";
- public User() { Console.WriteLine("UserObject Create..."); }
- public string UserName
- {
- get { return this._userName; }
- set { this._userName = value; }
- }
- public string Nick
- {
- get { return this._nick; }
- set { this._nick = value; }
- }
- public string Password = "";
- }
- public class Processer : IContextProperty, IContributeObjectSink
- {
- #region IContextProperty 成员
- public void Freeze(Context newContext)
- {
- }
- public bool IsNewContextOK(Context newCtx)
- {
- return true;
- }
- public string Name
- {
- get { return "mmcgzs.tracker"; }
- }
- #endregion
- #region IContributeObjectSink 成员
- public IMessageSink GetObjectSink(MarshalByRefObject obj, System.Runtime.Remoting.Messaging.IMessageSink nextSink)
- {
- return new TraceSink(obj, nextSink);
- }
- #endregion
- }
- public class TraceSink : IMessageSink
- {
- private User _sender;
- private IMessageSink _nextSink;
- public TraceSink(MarshalByRefObject sender, IMessageSink nextSink)
- {
- this._nextSink = nextSink;
- this._sender = sender as User;
- }
- #region IMessageSink 成员
- public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
- {
- return null;
- }
- public IMessageSink NextSink
- {
- get { return this._nextSink; }
- }
- public IMessage SyncProcessMessage(IMessage msg)
- {
- IMethodCallMessage call = msg as IMethodCallMessage;
- PropertyAction property = GetCallProperty(call);
- if (property != null)
- {
- Console.WriteLine(property.Action+" Property " + property.Name + " value " + property.Value);
- }
- return this._nextSink.SyncProcessMessage(call);
- }
- private PropertyAction GetCallProperty(IMethodCallMessage call)
- {
- PropertyAction p = new PropertyAction();
- if (call == null)
- return null;
- // 得到调用的方法的信息
- MethodBase methodInfo = call.MethodBase;
- if (methodInfo.Name.StartsWith("set_"))
- {
- p.Name = methodInfo.Name.Substring(4);
- p.Value = call.InArgs[0];
- p.Action = methodInfo.Name.Substring(0, 3);
- }
- else if (methodInfo.Name.StartsWith("get_"))
- {
- p.Name = methodInfo.Name.Substring(4);
- p.Value = methodInfo.Invoke(this._sender, null);
- p.Action = methodInfo.Name.Substring(0, 3);
- }
- else if (methodInfo.Name == "FieldSetter")
- {
- p.Name = (string)call.InArgs[1];
- p.Value = call.InArgs[2];
- p.Action = methodInfo.Name;
- }
- else if (methodInfo.Name == "FieldGetter")
- {
- p.Name = (string)call.InArgs[1];
- p.Value = this._sender.GetType().GetField(p.Name).GetValue(this._sender);
- p.Action = methodInfo.Name;
- }
- else
- {
- p = null;
- }
- return p;
- }
- #endregion
- }
- public class PropertyAction {
- public string Name = "";
- public object Value = null;
- public string Action = "";
- }
- class Program
- {
- static void Main(string[] args)
- {
- User user = new User();
- user.UserName = "mmcgzs";
- user.Nick = "毛毛虫";
- user.Password = "888888";
- string a = user.UserName;
- a = user.Password;
- Console.Read();
- }
- }
- }

浙公网安备 33010602011771号