在拦截方法调用时获取ContextBoundObject的类型

时间:2009-04-01 09:25:06

标签: c# .net

我正在拦截方法调用ContextBoundObject。有没有办法在消息接收器中获取我正在调用的对象的类型?

说我有一个班级

 [Intercept]
 [Synchronization]
 public class Test : ContextBoundObject
 {
    [Timeout(10010)]
    public void Method()
    {
        // Do something
    }
 }

在通话到Method之前的消息接收器中是否有某种方法可以获取Test类型,以便我可以查询自定义属性Timeout?例如

public IMessage SyncProcessMessage(IMessage msg)
{
      Type type = GetType(); // << Need to get hold of Test type here
      object[] custom = type.GetMethod("Method").GetCustomAttributes(false);;
      TimeoutAttribute ta = custom[0] as TimeoutAttribute;
      int time = ta.Ticks;

      IMessage returnedMessage = _nextSink.SyncProcessMessage(msg);
      return returnedMessage;
}

TA

1 个答案:

答案 0 :(得分:0)

经过一番混乱我找到了它。

类型名称将传递到IMessage的“属性”字典中的SyncProcessMessage中。

所以上面的代码变成了

public IMessage SyncProcessMessage(IMessage msg)
{
      Type type = Type.GetType(msg.Properties["__TypeName"].ToString());

      object[] custom = type.GetMethod("Method").GetCustomAttributes(false);
      TimeoutAttribute ta = custom[0] as TimeoutAttribute;
      int time = ta.Ticks;

      IMessage returnedMessage = _nextSink.SyncProcessMessage(msg);
      return returnedMessage;
}