在面向AOP开发时,定义的是SingleCall,服务器激活模式。
并且定义了interface供客户端和服务端同时使用,服务端实现该接口,
客户端通过URL创建相应接口类型的实例代理。
在访问时,程序可以正常访问,现希望通过:自定义上下文截获消息后,
主要是对IMethodMessage类型消息处理。
希望能获取消息访问的方法的所有属性(Attributes)
无论使用:
object [] attrs = msg.MethodBase.GetCustomAttributes(true);
还是使用
object [] attrs = msg.MethodBase.GetCustomAttributes(false);
都只能获取接口上定义的属性,而不能获取到相应在服务器上实例的属性。
现希望能获取实例类型相应方法上的属性。
本来在CSDN上咨询过该问题,后来找到一个妥协的办法:
我实现的方法如下:
public ServiceContextSink(IMessageSink nextSink)
{
WellKnownServiceTypeEntry[] wells;
wells = System.Runtime.Remoting.RemotingConfiguration.GetRegisteredWellKnownServiceTypes();
foreach (WellKnownServiceTypeEntry e in wells)
{
table["/"+e.ObjectUri] = Type.GetType(e.TypeName);
}
this.nextSink = nextSink;
}
private object[] GetCustomAttributes(string uri,IMethodMessage msg)
{
int index = uri.LastIndexOf('/');
string url = uri.Substring(index, uri.Length - index);
Type type = table[url] as Type;
Type[] types = new Type[msg.ArgCount];
index = 0;
for (int i = 0 ; i < msg.ArgCount ; i++)
{
Type t = msg.Args[i].GetType();
types[i] = t == null ? typeof(object) : t;
}
MethodInfo info = type.GetMethod(msg.MethodName, types);
if (info == null)
return new object[0];
return info.GetCustomAttributes(false);
}