private static RequestHandlerDelegate ForRequest4 <T, TParams, TResult, TResponseError>(Type targetType, MethodInfo method, HandlerProvider provider) where TResponseError : ResponseError, new() { Type deleType = typeof(Func <T, TParams, Result <TResult, TResponseError> >); Func <T, TParams, Result <TResult, TResponseError> > func = (Func <T, TParams, Result <TResult, TResponseError> >)method.CreateDelegate(deleType); return((r, c, t) => { RequestMessage <TParams> request = (RequestMessage <TParams>)r; object target = provider.CreateTargetObject(targetType, c, t); Result <TResult, TResponseError> result; try { result = func((T)target, request.Params); } catch (Exception ex) { Console.Error.WriteLine(ex); result = Result <TResult, TResponseError> .Error(Message.InternalError <TResponseError>()); } return new ResponseMessage <TResult, TResponseError> { Id = request.Id, Result = result.SuccessValue, Error = result.ErrorValue }; }); }
internal static NotificationHandlerDelegate CreateNotificationHandlerDelegate(Type targetType, MethodInfo method, HandlerProvider provider) { Type declaringType = method.DeclaringType; Type[] argTypes = method.GetParameters().Select(x => x.ParameterType).ToArray(); if (argTypes.Length > 1) { throw new ArgumentException($"signature mismatch: {method.Name}"); } if (method.ReturnType != typeof(void)) { throw new ArgumentException($"signature mismatch: {method.Name}"); } MethodInfo factory = argTypes.Length == 1 ? GetFactoryForNotification2(method, declaringType, argTypes[0]) : GetFactoryForNotification1(method, declaringType); return((NotificationHandlerDelegate)factory.Invoke(provider, new object[] { targetType, method, provider })); }
internal static RequestHandlerDelegate CreateRequestHandlerDelegate(Type targetType, MethodInfo method, HandlerProvider provider) { Type declaringType = method.DeclaringType; ParameterInfo[] parameters = method.GetParameters(); if (parameters.Length > 1) { throw new ArgumentException($"signature mismatch: {method.Name}"); } Type paramsType = parameters.Length == 1 ? parameters[0].ParameterType : null; Type returnType = method.ReturnType; Type openReturnType = returnType.GetGenericTypeDefinition(); Type resultType; Type responseErrorType; if (openReturnType == typeof(Result <,>)) { resultType = returnType.GenericTypeArguments[0]; responseErrorType = returnType.GenericTypeArguments[1]; } else if (returnType.GetGenericTypeDefinition() == typeof(VoidResult <>)) { resultType = null; responseErrorType = returnType.GenericTypeArguments[0]; } else { throw new ArgumentException($"signature mismatch: {method.Name}"); } MethodInfo factory = paramsType != null && resultType != null?GetFactoryForRequest4(method, declaringType, paramsType, resultType, responseErrorType) : paramsType == null && resultType != null?GetFactoryForRequest3(method, declaringType, resultType, responseErrorType) : GetFactoryForRequest2(method, declaringType, responseErrorType); return((RequestHandlerDelegate)factory.Invoke(provider, new object[] { targetType, method, provider })); }
private static NotificationHandlerDelegate ForNotification1 <T>(Type targetType, MethodInfo method, HandlerProvider provider) { Type deleType = typeof(Action <T>); Action <T> action = (Action <T>)method.CreateDelegate(deleType); return((n, c) => { VoidNotificationMessage notification = (VoidNotificationMessage)n; object target = provider.CreateTargetObject(targetType, c); try { action((T)target); } catch (Exception ex) { Console.Error.WriteLine(ex); } }); }
private static NotificationHandlerDelegate ForNotification2 <T, TParams>(Type targetType, MethodInfo method, HandlerProvider provider) { var deleType = typeof(Action <T, TParams>); var action = (Action <T, TParams>)method.CreateDelegate(deleType); return((n, c) => { var notification = (NotificationMessage <TParams>)n; var target = provider.CreateTargetObject(targetType, c); try { action((T)target, notification.@params); } catch (Exception ex) { Console.Error.WriteLine(ex); } }); }
private static RequestHandlerDelegate ForRequest2 <T, TResponseError>(Type targetType, MethodInfo method, HandlerProvider provider) where TResponseError : ResponseError, new() { var deleType = typeof(Func <T, VoidResult <TResponseError> >); var func = (Func <T, VoidResult <TResponseError> >)method.CreateDelegate(deleType); return((r, c, t) => { var request = (VoidRequestMessage)r; var target = provider.CreateTargetObject(targetType, c, t); VoidResult <TResponseError> result; try { result = func((T)target); } catch (Exception ex) { Console.Error.WriteLine(ex); result = VoidResult <TResponseError> .Error(Message.InternalError <TResponseError>()); } return new VoidResponseMessage <TResponseError> { id = request.id, error = result.ErrorValue }; }); }