jeudi 22 février 2018

Getting parameter type from proxy method and casting

I am using Castle DynamicProxy for creating a proxy for logging.

As a part of this I want to be able to grab the message ID (we are using RabbitMQ) to be able to log this as well.

The intercept is placed on a method that takes the following parameter:

ConsumeContext context

Within the context argument should be an object called Message which then contains the MessageId.

As the intercept is going over a network boundary, when we intercept the method and try to get the context argument, I believe because it's a generic it is coming out not as the right type and we therefore need to cast it. If I cast the parameter (invocation.Arguments[0] seen below) then I can see the Message object however it is a generic so I only know at run time what the type should be.

The image below shows the argument within the invocation (intercept) and the argument once the target method is invoked, i.e. one has the Message object one does not. If I cast the object then it works but I can't find anywhere to get the type.

castle issue

I've tried reflection with the following:

var invokeMethodParams = (((System.Reflection.MethodInfo)(invocation.Method))).GetParameters();
Type typetest = invokeMethodInfo [0].ParameterType;   

I then cast the object with typetest but it doesn't work it simply gives me the same kind of object I get in the first image, i.e. no Message object.





Aucun commentaire:

Enregistrer un commentaire