private string CreateMappingRegex(MethodInfo currentMethod, string mapping, IList <string> mappingTokens, IDictionary <int, Type> argumentsMapping) { for (int i = 0; i < mappingTokens.Count; i++) { if (mappingTokens[i].StartsWith("{") && mappingTokens[i].EndsWith("}")) { foreach (ParameterInfo parameterInfo in currentMethod.GetParameters()) { if (parameterInfo.GetCustomAttributes().All(x => x.GetType() != typeof(UriParameterAttribute))) { continue; } UriParameterAttribute uriParameter = parameterInfo.GetCustomAttribute <UriParameterAttribute>(); if (mappingTokens[i].Equals("{" + uriParameter.Value + "}")) { argumentsMapping.Add(i, parameterInfo.ParameterType); mapping = mapping.Replace(mappingTokens[i].ToString(), parameterInfo.ParameterType == typeof(string) ? "\\w+" : "\\d+"); break; } } } } return(mapping); }
public void Parse() { Type[] types = Assembly.GetExecutingAssembly().GetTypes(); foreach (Type type in types) { if (type.GetCustomAttributes().Any(x => x.GetType() == typeof(ControllerAttribute))) { foreach (var currentMethod in type.GetMethods(BindingFlags.Instance | BindingFlags.Public)) { if (currentMethod.GetCustomAttributes().Any(x => x.GetType() == typeof(RequestMappingAttribute))) { RequestMappingAttribute requestMapping = currentMethod.GetCustomAttribute <RequestMappingAttribute>(); RequestMethod requestMethod = requestMapping.Method; string mapping = requestMapping.Value; List <string> mappingTokens = mapping.Split('/').ToList(); Dictionary <int, Type> argumentsMapping = new Dictionary <int, Type>(); for (int i = 0; i < mappingTokens.Count; i++) { if (mappingTokens[i].StartsWith("{") && mappingTokens[i].EndsWith("}")) { foreach (ParameterInfo parameterInfo in currentMethod.GetParameters()) { if (parameterInfo.GetCustomAttributes().All(x => x.GetType() != typeof(UriParameterAttribute))) { continue; } UriParameterAttribute uriParameter = parameterInfo.GetCustomAttribute <UriParameterAttribute>(); if (mappingTokens[i].Equals("{" + uriParameter.Value + "}")) { argumentsMapping.Add(i, parameterInfo.ParameterType); mapping = mapping.Replace(mappingTokens[i].ToString(), parameterInfo.GetType() == typeof(string) ? "\\w+" : "\\d+"); break; } } } } Object controllerInstance = Activator.CreateInstance(type); ControllerActionPair pair = new ControllerActionPair(controllerInstance, currentMethod, argumentsMapping); if (!this.Controllers.ContainsKey(requestMethod)) { this.Controllers.Add(requestMethod, new Dictionary <string, ControllerActionPair>()); } this.Controllers[requestMethod].Add(mapping, pair); } } } else if (type.GetCustomAttributes().Any(x => x.GetType() == typeof(ComponentAttribute))) { foreach (Type parent in type.GetInterfaces()) { this.Components.Add(parent, type); } } } foreach (ControllerActionPair controllerActionPair in this.Controllers.Values.SelectMany(x => x.Values)) { this.ResolveDependencies(controllerActionPair.Controller); } }
public void Parse() { Type[] allTypesInCurrentExecutingAssembly = Assembly.GetExecutingAssembly().GetTypes(); Type[] controllerTypes = allTypesInCurrentExecutingAssembly .Where(t => t.GetCustomAttributes <ControllerAttribute>().Any()) .ToArray(); foreach (Type controllerType in controllerTypes) { var currentTypeMethods = controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public); foreach (var currentMethod in currentTypeMethods) { var isCurrentMethodForRequestMapping = currentMethod .GetCustomAttributes() .Any(x => x.GetType() == typeof(RequestMappingAttribute)); if (isCurrentMethodForRequestMapping) { RequestMappingAttribute methodRequestMappingAttribute = currentMethod.GetCustomAttribute <RequestMappingAttribute>(); RequestMethod requestMethod = methodRequestMappingAttribute.Method; string mapping = methodRequestMappingAttribute.Value; List <string> mappingTokens = mapping.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).ToList(); Dictionary <int, Type> argumentsMapping = new Dictionary <int, Type>(); for (int i = 0; i < mappingTokens.Count; i++) { var isTokenRequestParameter = mappingTokens[i].StartsWith("{") && mappingTokens[i].EndsWith("}"); if (isTokenRequestParameter) { foreach (ParameterInfo parameterInfo in currentMethod.GetParameters()) { int numberOfUriParameterAttributes = parameterInfo .GetCustomAttributes() .Count(x => x.GetType() == typeof(UriParameterAttribute)); if (numberOfUriParameterAttributes == 0) { continue; } UriParameterAttribute uriParameter = parameterInfo.GetCustomAttribute <UriParameterAttribute>(); bool isTargetMappingToken = mappingTokens[i].Equals("{" + uriParameter.Value + "}"); if (isTargetMappingToken) { argumentsMapping.Add(i, parameterInfo.ParameterType); string updatedMapping = mapping .Replace(mappingTokens[i].ToString(), parameterInfo.ParameterType == typeof(string) ? "\\w+" : "\\d+"); mapping = updatedMapping; break; } } } } Object controllerInstance = Activator.CreateInstance(controllerType); ControllerActionPair controllerActionPair = new ControllerActionPair(controllerInstance, currentMethod, argumentsMapping); if (!this.Controllers.ContainsKey(requestMethod)) { this.Controllers.Add(requestMethod, new Dictionary <string, ControllerActionPair>()); } this.Controllers[requestMethod].Add(mapping, controllerActionPair); } } } Type[] componentTypes = allTypesInCurrentExecutingAssembly .Where(t => t.GetCustomAttributes <ComponentAttribute>().Any()) .ToArray(); foreach (var componentType in componentTypes) { foreach (Type parent in componentType.GetInterfaces()) { this.Components.Add(parent, componentType); } } foreach (ControllerActionPair controllerActionPair in this.Controllers.Values.SelectMany(x => x.Values)) { this.ResolveDependencies(controllerActionPair.Controller); } }