/// <summary> /// GetLsonAttribute from Object /// </summary> /// <param name="attributes">CustomAttributes of object</param> /// <returns></returns> private static LsonAttribute GetLsonAttribute(object[] attributes) { LsonAttribute lsonAttribute = null; for (int i = 0, j = attributes.Length; i < j; i++) { if (attributes[i].GetType() == typeof(LsonAttribute)) { lsonAttribute = (LsonAttribute)attributes[i]; break; } } return(lsonAttribute); }
/// <summary> /// Set object value /// </summary> /// <param name="instance">object instance</param> /// <param name="adapter">Json Data Adapter</param> /// <param name="propertyInfos">propertyInfos of object</param> /// <param name="isOnlyLsonAttribute">Is only check LsonAttribute</param> private static void SetValues(LsonAdapter adapter, object instance, PropertyInfo[] propertyInfos, bool isOnlyLsonAttribute = false) { for (int i = 0, j = propertyInfos.Length; i < j; i++) { PropertyInfo propertyInfo = propertyInfos[i]; //false不查找继承链 object[] attributes = propertyInfo.GetCustomAttributes(false); LsonAttribute lsonAttribute = GetLsonAttribute(attributes); String jsonKey = propertyInfo.Name; if (lsonAttribute != null) { jsonKey = lsonAttribute.JsonKey; } else if (isOnlyLsonAttribute) { continue; } if (!adapter.Contains(jsonKey)) { continue; } object value = adapter.GetValue(jsonKey, propertyInfo.PropertyType); if (adapter.GetValueJsonType(jsonKey) == JsonType.Object) { object _instance = propertyInfo.GetValue(instance, null); if (isOnlyLsonAttribute) { value = FormJsonByLsonAttribute(_instance, (LsonAdapter)value); } else { value = FromJson(_instance, (LsonAdapter)value); } instanceLson.SetValue(instance, propertyInfo, value); } instanceLson.SetValue(instance, propertyInfo, value); } }