forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrmAop.cs
87 lines (75 loc) · 3.59 KB
/
OrmAop.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting;
using System;
using System.Reflection;
namespace CYQ.Data.Orm
{
class AopAttribute : ProxyAttribute
{
public override MarshalByRefObject CreateInstance(Type serverType)
{
AopProxy realProxy = new AopProxy(serverType, base.CreateInstance(serverType));
return realProxy.GetTransparentProxy() as MarshalByRefObject;
}
}
class AopProxy : RealProxy
{
MethodInfo method;
MarshalByRefObject _target = null;
public AopProxy(Type serverType, MarshalByRefObject target)
: base(serverType)
{
_target = target;
method = serverType.GetMethod("Set", BindingFlags.NonPublic | BindingFlags.Instance);
}
public override IMessage Invoke(IMessage msg)
{
if (msg != null)
{
if (msg is IConstructionCallMessage)
{
IConstructionCallMessage constructCallMsg = msg as IConstructionCallMessage;
//IConstructionReturnMessage constructionReturnMessage = this.InitializeServerObject((IConstructionCallMessage)msg);
//RealProxy.SetStubData(this, constructionReturnMessage.ReturnValue);
//return constructionReturnMessage;
RealProxy defaultProxy = RemotingServices.GetRealProxy(_target);
//如果不做下面这一步,_target还是一个没有直正实例化被代理对象的透明代理,
//这样的话,会导致没有直正构建对象。
defaultProxy.InitializeServerObject(constructCallMsg);
//本类是一个RealProxy,它可通过GetTransparentProxy函数得到透明代理
return System.Runtime.Remoting.Services.EnterpriseServicesHelper.CreateConstructionReturnMessage(constructCallMsg, (MarshalByRefObject)GetTransparentProxy());
}
else if (msg is IMethodCallMessage)
{
IMethodCallMessage callMsg = msg as IMethodCallMessage;
object[] args = callMsg.Args;
//System.Windows.Forms.MessageBox.Show(callMsg.MethodBase.ToString());
if (callMsg.MethodName.StartsWith("set_") && args.Length == 1)
{
method.Invoke(_target, new object[] { callMsg.MethodName.Substring(4), args[0] });//对属性进行调用
}
return RemotingServices.ExecuteMessage(_target, callMsg);
//IMessage message = null;
//try
//{
// MarshalByRefObject obj = GetUnwrappedServer();
// if (callMsg.MethodName.StartsWith("set_") && args.Length == 1)
// {
// method.Invoke(obj, new object[] { callMsg.MethodName.Substring(4), args[0] });//对属性进行调用
// }
// object o = callMsg.MethodBase.Invoke(obj, args);
// message = new ReturnMessage(o, args, args.Length, callMsg.LogicalCallContext, callMsg);
//}
//catch (Exception e)
//{
// message = new ReturnMessage(e, callMsg);
//}
//return message;
}
}
return msg;
}
}
}