public ProxyParameterData(ProxyParameterRestType restType, Type parameterType, string lookName, Func <string, object> parse) { this.restType = restType; this.parameterType = parameterType; this.lookName = lookName; this.parse = parse; }
/// <summary> /// Create new parameter for proxy injection /// </summary> /// <param name="restType">REST parameter type</param> /// <param name="parameterType">Parameter type</param> /// <param name="paramterName">Name</param> /// <param name="parser">Paramter value parser</param> public ProxyParameterData(ProxyParameterRestType restType, Type parameterType, string paramterName, Func <string, object> parser) { this._restType = restType; this._parameterType = parameterType; this._paramterName = paramterName; this._parser = parser; }
internal RestProxy(Type restClass, Dictionary <string, Object> injectables) { instance = Activator.CreateInstance(restClass); classType = restClass; Attribute t = restClass.GetCustomAttribute(typeof(REST)); Logger.Log("RestProxy", $"Creating proxy for {restClass.Name}"); proxyMethods = new Dictionary <string, ProxyMethod>(); REST trest = (REST)t; // Search Injectables to inject FieldInfo[] fields = restClass.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); foreach (FieldInfo field in fields) { if (field.GetCustomAttribute(typeof(Inject)) != null) { Type ft = field.FieldType; Object injectableInstance; if (injectables.ContainsKey(ft.FullName)) { injectableInstance = injectables[ft.FullName]; } else { Logger.Log("RestProxy", $"Creating injectable instance for class {ft.FullName}"); injectableInstance = Activator.CreateInstance(ft); injectables.Add(ft.FullName, injectableInstance); } field.SetValue(instance, injectableInstance); } } // Search Methods to Map MethodInfo[] methods = restClass.GetMethods(); foreach (var methodInfo in methods) { proxyMethods.Add(methodInfo.Name, new ProxyMethod(methodInfo)); foreach (var paramInfo in methodInfo.GetParameters()) { // Default to body param ProxyParameterRestType restType = ProxyParameterRestType.BODY; string lookName = paramInfo.Name; Attribute p; if ((p = paramInfo.GetCustomAttribute(typeof(QueryParam))) != null) { restType = ProxyParameterRestType.QUERY; lookName = ((QueryParam)p).ParamName ?? paramInfo.Name; } Func <string, object> parser; Type baseType; if ((baseType = GetBaseType(paramInfo.ParameterType)) != null) { parser = x => { object[] dp = { x, Activator.CreateInstance(baseType) }; baseType.InvokeMember("TryParse", BindingFlags.InvokeMethod, null, null, dp); return(dp[1]); }; } else if (typeof(string).IsAssignableFrom(paramInfo.ParameterType)) { parser = x => x; } else { parser = x => JsonConvert.DeserializeObject(x, paramInfo.ParameterType); } proxyMethods[methodInfo.Name].ProxyData.Add(new ProxyParameterData(restType, paramInfo.ParameterType, lookName, parser)); } } }