2
我需要通過RealProxy調用帶ref-arguments的方法。我已經將問題隔離到以下代碼:通過RealProxy參考參數
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);
}
}
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!";
}
}
}
該程序應產生「Hello World!」輸出,但不知何故ref參數的修改會在代理中丟失。我需要做些什麼才能使其發揮作用?
非常感謝!這非常有幫助。 – Guge 2010-06-21 07:07:20
謝謝,它像一個魅力 – phuongnd 2016-07-05 04:35:05