internal static MethodMap <T> Create(Type modelType) { var methodMap = new Dictionary <string, OperationInfo <T> >(); foreach (var methodInfo in modelType.GetRuntimeMethods()) { if (methodInfo.IsPrivate) { continue; } var operationAttribute = GetOperationAttribute(methodInfo); var methodName = methodInfo.Name; var operationInfo = operationAttribute.ToOperationInfo <T>(methodInfo); if (operationAttribute.Type != OperationType.Disallowed) { Validate(methodInfo); } //For backward compatibility, add using just name. //Before overload support, only the method name was recorded to the journal. //This will support the cases where there are no overloads. if (!methodMap.ContainsKey(methodName)) { methodMap.Add(methodName, operationInfo); } // For backward compatibility: //Handle the case when overloads are introduced to an existing model. The user can //map a method name in the journal to a specific overload var commandAttribute = operationAttribute as CommandAttribute; if (commandAttribute != null && commandAttribute.IsDefault) { //ensure there is only one method in the group marked default if (methodMap.ContainsKey(methodName)) { var previousOperation = methodMap[methodName].OperationAttribute as CommandAttribute; if (previousOperation != null && previousOperation.IsDefault) { throw new Exception("Only one method per group can be marked IsDefault"); } } methodMap[methodName] = operationInfo; } //use a unique signature based on the method name and argument types var signature = methodInfo.ToString(); methodMap.Add(signature, operationInfo); } var result = new MethodMap <T>(methodMap); return(result); }
public override object Execute(TModel model) { try { var proxyMethod = MethodMap.MapFor <TModel>().GetOperationInfo(MethodName); var methodInfo = proxyMethod.MethodInfo; if (methodInfo.IsGenericMethod) { methodInfo = methodInfo.MakeGenericMethod(GenericTypeArguments); } return(methodInfo.Invoke(model, Arguments)); } catch (TargetInvocationException ex) { throw ex.InnerException; } }
public void SetClient(Client <TModel> client) { _handler = client; _methods = MethodMap.MapFor <TModel>(); }