English 中文(简体)
通过实际程序提出抗辩
原标题:Ref argument through a 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”产出,但在某种程度上,替代论点的修改在代理中消失。 我必须做些什么才能做到这一点?

最佳回答

回返的第二个参数需要包含回归的价值观,并排除参数以回去。 你们可以通过在以下几处提及你所穿的阵列来实现:

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);
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签