开发者

Ref argument through a RealProxy

I need to call a method with ref-arguments through a RealProxy. I have isolated the problem down to the following code:

using System;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             HelloClass hello=new HelloClass();
             TestProxy proxy = new TestProxy(hello);
             HelloClass hello2 = proxy.GetTransparentProxy() as HelloClass;

             string t = "";
             hello2.SayHello(ref t);
             Console.Out.WriteLine(t);
        }
开发者_如何学C    }

    public class TestProxy : RealProxy
    {
         HelloClass _hello;

         public TestProxy(HelloClass hello)
             : base(typeof(HelloClass))
         {
             this._hello = hello;
         }

         public override System.Runtime.Remoting.Messaging.IMessage Invoke(System.Runtime.Remoting.Messaging.IMessage msg)
        {
            IMethodCallMessage call = msg as IMethodCallMessage;
            object returnValue = typeof(HelloClass).InvokeMember(call.MethodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, _hello, call.Args);
            return new ReturnMessage(returnValue, null, 0, call.LogicalCallContext, call);
        }
    }

    public class HelloClass : MarshalByRefObject
    {
        public void SayHello(ref string s)
        {
            s = "Hello World!";
        }
    }
}

The program should produce the "Hello World!" output, but somehow the modification of the ref arguments gets lost in the proxy. What do I have to do to get this to work?


The second parameter of ReturnMessage needs to contain the values of the ref and out parameters to pass back. You can get them by saving a reference to the array you pass in:

public override System.Runtime.Remoting.Messaging.IMessage Invoke(System.Runtime.Remoting.Messaging.IMessage msg)
{
    IMethodCallMessage call = msg as IMethodCallMessage;
    var args = call.Args;
    object returnValue = typeof(HelloClass).InvokeMember(call.MethodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, _hello, args);
    return new ReturnMessage(returnValue, args, args.Length, call.LogicalCallContext, call);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜