Remoting中,关于获取某接口派生的自定义属性(CustomAttribute)的问题

在面向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);
        }

转载于:https://www.cnblogs.com/begincsdn/archive/2007/01/25/630415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值