コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }