动态执行代理

时间:2012-11-19 16:03:35

标签: c# dynamic reflection delegates

我有一个基本上是消息处理程序的类,它接受请求,找到该消息类型的处理器,创建适当的响应并返回它。为此,我有 创建了一个委托如下public delegate Rs ProcessRequest<Rq,Rs>(Rq request);然后在我的类中创建了一些支持的消息及其处理方法。问题是主要的处理方法应该找出使用哪种处理方法无法使用GetMethod()方法找到方法。

以下是整个代码,如果你能告诉我如何动态选择合适的方法然后执行它,那就是我正在寻找的东西。

public delegate Rs ProcessRequest<in Rq, out Rs>(Rq request) where Rq : API.Request where Rs : API.Response;

public class WebSocketServer
{
    private WebSocketMessageHandler messageHander;

    // Incoming message handlers
    public ProcessRequest<InitUDPConnectionRq, InitUDPConnectionRs> ProcessInitUDPConnection;
    public ProcessRequest<ListenHandshakeRq, ListenHandshakeRs> ProcessListenHandshake;
    public ProcessRequest<PresenceChangeRq, PresenceChangeRs> ProcessPresenceChange;
    public ProcessRequest<ChatMessageRq, ChatMessageRs> ProcessChatMessage;
    public ProcessRequest<RDPRequestResponseRq, RDPRequestResponseRs> ProcessRDPRequestResponse;
    public ProcessRequest<RDPIncomingRequestRq, RDPIncomingRequestRs> ProcessRDPIncomingRequest;

    public WebSocketServer(WebSocketMessageHandler handler)
    {
        this.messageHander = handler;
    }

    public void processRequest(API.Request request)
    {
        String resquestType = request.GetType().Name;
        String processorName = resquestType.Substring(0, resquestType.Length - 2);
        API.Response response = null;
        // Do we have a process method for this processor
        MethodInfo methodInfo = this.GetType().GetMethod("Process" + processorName);
        if (methodInfo != null)
        {
             // Execute the method via Invoke...., but code never gets here
        }
        else
        {
            logger.Warn("Failed to find a processor for " + processorName);
            response = new ErrorRs(request.id, "Failed to find a processor for " + processorName);
        }
        sendResponse(response, request);
    }
}

现在我将这些字段分配给方法,我只是无法动态执行它们。

// Link into the hooks so we can receive requests
_appContext.ConnectionManager.Connection.webSocketServer.ProcessInitUDPConnection = ProcessInitUDPConnection;
_appContext.ConnectionManager.Connection.webSocketServer.ProcessListenHandshake = ProcessListenHandshake;
_appContext.ConnectionManager.Connection.webSocketServer.ProcessPresenceChange = ProcessPresenceChange;
_appContext.ConnectionManager.Connection.webSocketServer.ProcessChatMessage = ProcessChatMessage;

// 1 method as an example
private PresenceChangeRs ProcessPresenceChange(PresenceChangeRq request)
{
    _appContext.RosterManager.presenceChange(request.user, request.presence);
    return new PresenceChangeRs();
}

1 个答案:

答案 0 :(得分:1)

以下是Dictionary用法的示例代码。对我来说有太多的自定义类型,以确保它完全编译和测试,但应该让你走在正确的轨道上。

public class WebSocketServer
{
    private WebSocketMessageHandler messageHander;

    // Incoming message handlers
    private Dictionary<string, System.Delegate> ProcessHandlers = new Dictionary<string, System.Delegate>();

    public void RegisterProcessHandler(string name, System.Delegate handler)
    {
        ProcessHandlers.Add(name, handler);
    }

    public void processRequest(API.Request request)
    {
        String resquestType = request.GetType().Name;
        String processorName = resquestType.Substring(0, resquestType.Length - 2);
        API.Response response = null;

        string processorName = "Process" + processorName;

        if (ProcessHandlers.ContainsKey(processorName))
        {
            System.Delegate myDelegate = ProcessHandlers[processorName]; 
            response = (API.Response)myDelegate.DynamicInvoke(request);
        }
        else
        {
            logger.Warn("Failed to find a processor for " + processorName);
            response = new ErrorRs(request.id, "Failed to find a processor for " + processorName);
        }

        sendResponse(response, request);
    }
}

注册:

var webSocketServer = _appContext.ConnectionManager.Connection.webSocketServer;
webSocketServer.RegisterProcessHandler("InitUDPConnection", ProcessInitUDPConnection);
webSocketServer.RegisterProcessHandler("ListenHandshake", ProcessListenHandshake);