Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# RealProxy: generic methods?

I'm trying to handle a call of a generic method through a RealProxy, but I cannot seem to find the information about the actual type of the generic parameter used in the intercepted method call. An excerpt of the code:

    public override IMessage Invoke(IMessage msg)
    {
        ...
        string methodName = (string)msg.Properties["__MethodName"];
        Type[] parameterTypes = (Type[])msg.Properties["__MethodSignature"];
        object[] args = (object[])msg.Properties["__Args"];

        MethodInfo method = typeToProxy.GetMethod(methodName, parameterTypes);
        ...

Let's say I'm proxying an interface like

interface IFactory
{
   TService Create<TService>()
}

When I call the proxy

proxied.Create<MyClass>()

I want to be able to find out the generic parameter is of type MyClass. Is this possible through RealProxy?

like image 237
Igor Brejc Avatar asked Oct 31 '10 07:10

Igor Brejc


2 Answers

There is an excellent MSDN article about RealProxy which I recommend you read. Among other things, it introduces MethodCallMessageWrapper which saves you the trouble of working directly against the Properties dictionary. From the latter you can get the MethodBase, which in turn contains the generic arguments:

internal class MyProxy : RealProxy
{
   private object m_instance;    
   private MyProxy( object instance ) : base( typeof( IFactory) )
   {
      m_instance = instance;
   }

  public override IMessage Invoke( IMessage message )
  {
     IMethodCallMessage methodMessage =
        new MethodCallMessageWrapper( (IMethodCallMessage) message );

     // Obtain the actual method definition that is being called.
     MethodBase method = methodMessage.MethodBase;

     Type[] genericArgs = method.GetGenericArguments(); //This is what you want

     return new ReturnMessage(...);
  }

  ...
}
like image 142
Ohad Schneider Avatar answered Oct 09 '22 15:10

Ohad Schneider


For method calls, the IMessage argument should be a IMethodMessage, which has a MethodBase property:

public override IMessage Invoke(IMessage message)
{
    IMethodMessage methodMessage = message as IMethodMessage;
    if (methodMessage != null)
    {
         MethodBase method = methodMessage.MethodBase;
         Type[] genericArgs = method.GetGenericArguments();

         ...
    }
    else
    {
        // not a method call
    }
}
like image 36
Ruben Avatar answered Oct 09 '22 15:10

Ruben