public static string GetPropertyKey(System.Reflection.MemberInfo prop, EntityClassAttribute classAttribute) { string text = prop.Name; if (classAttribute != null && classAttribute.LowerCaseKey) { text = text.ToLower(); } object[] customAttributes = prop.GetCustomAttributes(typeof(FieldAttribute), true); string result; if (customAttributes == null) { result = text; } else { object[] array = customAttributes; for (int i = 0; i < array.Length; i++) { object obj = array[i]; FieldAttribute fieldAttribute = obj as FieldAttribute; if (fieldAttribute != null) { if (!string.IsNullOrEmpty(fieldAttribute.FieldName)) { text = fieldAttribute.FieldName; } } } result = text; } return(result); }
public static EntityClassAttribute GetEntityClassAttribute(System.Type type) { object[] customAttributes = type.GetCustomAttributes(typeof(EntityClassAttribute), true); EntityClassAttribute result = null; if (customAttributes != null && customAttributes.Length > 0) { result = (customAttributes[0] as EntityClassAttribute); } return(result); }
private static TData GetEntityData <TData>(object entity, System.Type entityType, Action <TData, string, object> action) where TData : class, new() { TData result; if (entity == null) { result = default(TData); } else { TData tData = System.Activator.CreateInstance <TData>(); EntityClassAttribute entityClassAttribute = JavaScriptSerializer.GetEntityClassAttribute(entityType); System.Reflection.PropertyInfo[] properties = entityType.GetProperties(); System.Reflection.PropertyInfo[] array = properties; for (int i = 0; i < array.Length; i++) { System.Reflection.PropertyInfo propertyInfo = array[i]; string propertyKey = JavaScriptSerializer.GetPropertyKey(propertyInfo, entityClassAttribute); object value = propertyInfo.GetValue(entity, null); if (value == null) { action(tData, propertyKey, value); } else { System.Type propertyType = propertyInfo.PropertyType; if (propertyType.IsGenericType) { if (EntityFactory.IsIList(propertyType)) { System.Type type = propertyType.GetGenericArguments()[0]; if (type.IsClass) { System.Collections.IList entites = value as System.Collections.IList; System.Collections.Generic.IList <TData> entityDatas = EntityFactory.GetEntityDatas <TData>(entites, type, action); action(tData, propertyKey, entityDatas); } } } else if (!EntityFactory.IsPrimitiveType(propertyInfo.PropertyType)) { action(tData, propertyKey, EntityFactory.GetEntityData <TData>(value, propertyInfo.PropertyType, action)); } else { action(tData, propertyKey, value); } } } result = tData; } return(result); }
private static object GetEntity <TData>(TData data, System.Type entityType, Func <TData, string, object> func) where TData : class { object result; if (data == null) { result = null; } else { EntityClassAttribute entityClassAttribute = JavaScriptSerializer.GetEntityClassAttribute(entityType); System.Reflection.PropertyInfo[] properties = entityType.GetProperties(); object obj = System.Activator.CreateInstance(entityType); System.Reflection.PropertyInfo[] array = properties; for (int i = 0; i < array.Length; i++) { System.Reflection.PropertyInfo propertyInfo = array[i]; System.Reflection.MethodInfo setMethod = propertyInfo.GetSetMethod(); if (!(setMethod == null)) { string propertyKey = JavaScriptSerializer.GetPropertyKey(propertyInfo, entityClassAttribute); System.Type propertyType = propertyInfo.PropertyType; object value = EntityFactory.GetValue(func(data, propertyKey), propertyType); if (value != null) { if (propertyType.IsGenericType) { if (EntityFactory.IsIList(propertyType)) { System.Type type = propertyType.GetGenericArguments()[0]; propertyInfo.SetValue(obj, EntityFactory.GetEntities <TData>(value as System.Collections.IEnumerable, type, func), null); } } else if (!EntityFactory.IsPrimitiveType(propertyType)) { propertyInfo.SetValue(obj, EntityFactory.GetEntity <TData>(value as TData, propertyType, func), null); } else { setMethod.Invoke(obj, new object[] { value }); } } } } result = obj; } return(result); }
private void SerializeCustomObject(object o, System.Text.StringBuilder sb, int depth, System.Collections.Hashtable objectsInUse, JavaScriptSerializer.SerializationFormat serializationFormat) { bool flag = true; System.Type type = o.GetType(); sb.Append('{'); if (this.TypeResolver != null) { string text = this.TypeResolver.ResolveTypeId(type); if (text != null) { JavaScriptSerializer.SerializeString("__type", sb); sb.Append(':'); this.SerializeValue(text, sb, depth, objectsInUse, serializationFormat); flag = false; } } EntityClassAttribute entityClassAttribute = JavaScriptSerializer.GetEntityClassAttribute(type); System.Reflection.FieldInfo[] fields = type.GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); System.Reflection.FieldInfo[] array = fields; for (int i = 0; i < array.Length; i++) { System.Reflection.FieldInfo fieldInfo = array[i]; if (!fieldInfo.IsDefined(typeof(ScriptIgnoreAttribute), true)) { if (!flag) { sb.Append(','); } JavaScriptSerializer.SerializeString(JavaScriptSerializer.GetPropertyKey(fieldInfo, entityClassAttribute), sb); sb.Append(':'); this.SerializeValue(fieldInfo.GetValue(o), sb, depth, objectsInUse, serializationFormat); flag = false; } } System.Reflection.PropertyInfo[] properties = type.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.GetProperty); System.Reflection.PropertyInfo[] array2 = properties; for (int i = 0; i < array2.Length; i++) { System.Reflection.PropertyInfo propertyInfo = array2[i]; if (!propertyInfo.IsDefined(typeof(ScriptIgnoreAttribute), true)) { System.Reflection.MethodInfo getMethod = propertyInfo.GetGetMethod(); if (!(getMethod == null)) { if (getMethod.GetParameters().Length <= 0) { if (!flag) { sb.Append(','); } JavaScriptSerializer.SerializeString(JavaScriptSerializer.GetPropertyKey(propertyInfo, entityClassAttribute), sb); sb.Append(':'); this.SerializeValue(getMethod.Invoke(o, null), sb, depth, objectsInUse, serializationFormat); flag = false; } } } } sb.Append('}'); }