public static Func <object[], object> RegisterDelegate <T>(string id, Expression <T> expression) where T : Delegate { if (string.IsNullOrEmpty(id)) { throw new ArgumentException(nameof(id)); } if (expression == null) { throw new ArgumentNullException(nameof(expression)); } var methodInfo = MethodUtil.GetMethodInfo(expression); Func <object[], object> func; if (methodInfo.ReturnType != typeof(void)) { func = DelegateFactory.CreateValueDelegate(expression, methodInfo); } else { var dge = DelegateFactory.CreateVoidDelegate(expression, methodInfo); func = (args) => { dge.Invoke(args); return(null); }; } RegisterMethod(id, func); return(func); }
public static Method Register( string methodId, Expression<Action> implementExpression){ if (implementExpression == null) { throw new ArgumentNullException(nameof(implementExpression)); } var method = MethodUtil.GetMethodInfo(implementExpression); if (typeof(Delegate).IsAssignableFrom(method.DeclaringType)) { throw new ArgumentException($"{nameof(implementExpression)}不能为Delegate!"); } Func<object[], object> func = null; if (method.IsStatic) { if (method.ReturnType == typeof(void)) { func = RpcRegister.RegisterStaticVoidMethod(methodId, method); } else { func = RpcRegister.RegisterStaticValueMethod(methodId, method); } } else { if (method.ReturnType == typeof(void)) { func = RpcRegister.RegisterVoidMethod(methodId, GetObject(implementExpression), method); } else { func = RpcRegister.RegisterValueMethod(methodId, GetObject(implementExpression), method); } } return new Method { Id = methodId, Function = func }; }
public static string GetId <T>(Expression <T> expression) { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } var method = MethodUtil.GetMethodInfo(expression); var response = method.GetCustomAttribute <RpcResponseAttribute>(); if (response == null) { throw new MissingAttributeException($"{method.Name}缺少{nameof(RpcResponseAttribute)}属性"); } return(response.GetId()); }
public static Method Register<TDelegate>( string methodId, Expression<TDelegate> implementExpression) where TDelegate : Delegate { if (implementExpression == null) { throw new ArgumentNullException(nameof(implementExpression)); } var method = MethodUtil.GetMethodInfo(implementExpression); if (!typeof(Delegate).IsAssignableFrom(method.DeclaringType)) { throw new ArgumentException($"{nameof(implementExpression)}不是delegate类型!"); } var func = RpcRegister.RegisterDelegate(methodId, implementExpression); return new Method { Id = methodId, Function = func }; }
public static MethodTuple CreateMethodTuple <T>(Expression <T> expression) { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } var method = MethodUtil.GetMethodInfo(expression); var response = method.GetCustomAttribute <RpcRequestAttribute>(); if (response == null) { throw new MissingAttributeException($"{method.Name}缺少{nameof(RpcRequestAttribute)}属性"); } var dge = CreateDelegate(expression); var methodTupe = new MethodTuple() { RpcMethod = response, Action = dge }; return(methodTupe); }