internal IDictionary <string, object> CreateParameterDictionary() { var dict = new Dictionary <string, object>(); // Use reflection to build the parameter dictionary. foreach (PropertyInfo property in GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)) { // Retrieve the attribute. RequestParameterAttribute requestParameterAttribute = property.GetCustomAttributes(typeof(RequestParameterAttribute), false).FirstOrDefault() as RequestParameterAttribute; if (requestParameterAttribute == null) { continue; } // Get the discovery name of this parameter by looking at the attribute, or taking a lower-case // variant of the property name if no special name was set. string name = requestParameterAttribute.Name ?? property.Name.ToLower(); // Set the value in the dictionary. var propertyType = property.PropertyType; var value = property.GetValue(this, null); if (propertyType.IsValueType || value != null) { dict.Add(name, value); } } return(dict); }
/// <summary> /// Iterates over all <see cref="Google.Apis.Util.RequestParameterAttribute"/> properties in the request /// object and invokes the specified action for each of them. /// </summary> /// <param name="request">A request object</param> /// <param name="action">An action to invoke which gets the parameter type, name and its value</param> private static void IterateParameters(object request, Action <RequestParameterType, string, object> action) { // Use reflection to build the parameter dictionary. foreach (PropertyInfo property in request.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)) { // Retrieve the RequestParameterAttribute. RequestParameterAttribute attribute = property.GetCustomAttributes(typeof(RequestParameterAttribute), false).FirstOrDefault() as RequestParameterAttribute; if (attribute == null) { continue; } // Get the name of this parameter from the attribute, if it doesn't exist take a lower-case variant of // property name. string name = attribute.Name ?? property.Name.ToLower(); var propertyType = property.PropertyType; var value = property.GetValue(request, null); // Call action with the type name and value. if (propertyType.IsValueType || value != null) { action(attribute.Type, name, value); } } }
[TestCase("TestValue", "TESTVALUE", true, "TESTVALUE")]//TO discuss: Should the Uppercasing be before Value test or after? public void GetPropertyValueTest(string propertyReturnValue, string ignoreValue, bool uppercase, string expected) { object emptyTestObject = new object(); Mock <PropertyInfo> propertyInfoMock = new Mock <PropertyInfo>(); propertyInfoMock.Setup(p => p.GetValue(emptyTestObject, null)).Returns(propertyReturnValue); RequestParameterAttribute attr = new RequestParameterAttribute("Test", ignoreValue, uppercase); var actual = attr.GetPropertyValue(propertyInfoMock.Object, emptyTestObject); Assert.AreEqual(expected, actual); }
private static string GetPropertyName(PropertyInfo property, RequestParameterAttribute parameterAttribute) { var isIdCollection = property.CustomAttributes .Any(x => x.AttributeType == typeof(IdCollectionAttribute)); return(parameterAttribute == null && isIdCollection? property.Name.Replace("Id", string.Empty) : parameterAttribute == null ? property.Name : parameterAttribute.EntityProperty); }
/// <summary> /// Iterates over all <see cref="Google.Apis.Util.RequestParameterAttribute"/> properties in the request /// object and invokes the specified action for each of them. /// </summary> /// <param name="request">A request object</param> /// <param name="action">An action to invoke which gets the parameter type, name and its value</param> private static void IterateParameters(object request, Action <RequestParameterType, string, object> action) { // Use reflection to build the parameter dictionary. foreach (PropertyInfo property in request.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)) { // Retrieve the RequestParameterAttribute. RequestParameterAttribute attribute = property.GetCustomAttributes(typeof(RequestParameterAttribute), false).FirstOrDefault() as RequestParameterAttribute; if (attribute == null) { continue; } // Get the name of this parameter from the attribute, if it doesn't exist take a lower-case variant of // property name. string name = attribute.Name ?? property.Name.ToLower(); var propertyType = property.PropertyType; var value = property.GetValue(request, null); // Call action with the type name and value. if (propertyType.GetTypeInfo().IsValueType || value != null) { if (attribute.Type == RequestParameterType.UserDefinedQueries) { if (typeof(IEnumerable <KeyValuePair <string, string> >).IsAssignableFrom(value.GetType())) { foreach (var pair in (IEnumerable <KeyValuePair <string, string> >)value) { action(RequestParameterType.Query, pair.Key, pair.Value); } } else { Logger.Warning("Parameter marked with RequestParameterType.UserDefinedQueries attribute " + "was not of type IEnumerable<KeyValuePair<string, string>> and will be skipped."); } } else { action(attribute.Type, name, value); } } } }
/// <summary> /// 检验参数是否为必填项 /// </summary> public virtual void Validate() { RequestEntityInfo requestEntityInfo = RequestEntityManager.GetBy(this.GetType()); foreach (var item in requestEntityInfo.RequestParameterDict) { string propertyName = item.Key; RequestParameterAttribute parameterAttritube = item.Value; if (parameterAttritube.IsRequire) { int piIndex = requestEntityInfo.RequestIndexDict[propertyName]; object o = requestEntityInfo.PiArray[piIndex].GetValue(this, null); if (o == null || o.ToString() == string.Empty) { throw new RequestParameterIsRequireException(propertyName); } } } }
/// <summary> /// 获取请求参数字典集合 /// </summary> /// <returns></returns> public virtual Dictionary <string, string> GetParameterDict() { Dictionary <string, string> result = new Dictionary <string, string>(); RequestEntityInfo requestEntityInfo = RequestEntityManager.GetBy(this.GetType()); foreach (var item in requestEntityInfo.RequestParameterDict) { string propertyName = item.Key; RequestParameterAttribute parameterAttritube = item.Value; string parameterName = parameterAttritube.Name; int piIndex = requestEntityInfo.RequestIndexDict[propertyName]; object value = requestEntityInfo.PiArray[piIndex].GetValue(this, null); if (value != null && value.ToString() != string.Empty) { result.Add(parameterName, value.ToString()); } } return(result); }
/// <summary> /// 通过反射获取type类型的必要信息 /// </summary> /// <param name="type"></param> /// <returns></returns> internal static RequestEntityInfo Generate(Type type) { RequestEntityInfo result = new RequestEntityInfo { RequestType = type, PiArray = type.GetProperties() }; for (int i = 0; i < result.PiArray.Length; i++) { RequestParameterAttribute attritube = Attribute.GetCustomAttribute(result.PiArray[i], typeof(RequestParameterAttribute)) as RequestParameterAttribute; if (attritube != null) { string propertyName = result.PiArray[i].Name; result.RequestParameterDict.Add(propertyName, attritube); result.RequestIndexDict.Add(propertyName, i); continue; } } return(result); }