1

我正在尝试挂钩事件(在这种情况下为操作),但无法无一例外地从代理返回。想法是这样的:我有事件接口,它们没有在客户端订阅。因此,当我尝试在客户端引发事件时,我在代理中捕获此事件并将此事件的名称及其参数发送到我的远程服务器,我只想从代理返回。这是我的样本

public interface IMyEvents
{
    Action OnPing { get; set; }
}

public class Proxy : RealProxy
{
    Type type;

    public Proxy(Type type)
        : base(type)
    {
        this.type = type;
    }

    public override IMessage Invoke(IMessage msg)
    {
        var call = (IMethodCallMessage)msg;
        string MethodName = (string)msg.Properties["__MethodName"];
        object[] parameters = (object[])msg.Properties["__Args"];

        // Send Command to server
        // SendData(MethodName, parameters);

        // tell the invoker that everything's fine
        return new ReturnMessage(null, null, 0, call.LogicalCallContext, call);
    }
}

public class Test
{
    public Test()
    {
        Proxy proxy = new Proxy(typeof(IMyEvents));
        Events = (IMyEvents)proxy.GetTransparentProxy();
    }

    public readonly IMyEvents Events;
}

class Program
{
    public static void Main(string[] args)
    {
        Test t = new Test();
        t.Events.OnPing();  // NullReferenceException
    }
}
4

0 回答 0