Sunday, April 17, 2011

Getting the Type of a ContextBoundObject when intercepting a method call

I'm intercepting method calls into a ContextBoundObject. Is there a way to get hold of the Type of the object I'm calling when in the message sink?

Say I've got a class

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

In the message sink before the call is put through to Method is there someway to get hold of the Test type so I can query the custom attribute Timeout? eg

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

From stackoverflow
  • After a little messing I found it.

    The type name is passed into SyncProcessMessage in the Properties dictionary in IMessage.

    So the code above becomes

    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;
    }
    

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.