コード例 #1
0
        /// <summary>
        /// 动态生成getvalue
        /// </summary>
        /// <returns></returns>
        private static Func <object, string, object> GenerateGetValue()
        {
            var instance   = Expression.Parameter(typeof(object), "instance");
            var memberName = Expression.Parameter(typeof(string), "memberName");
            var nameHash   = Expression.Variable(typeof(int), "nameHash");
            var calHash    = Expression.Assign(nameHash, Expression.Call(memberName, typeof(object).GetMethod("GetHashCode")));
            var cases      = new List <SwitchCase>();

            foreach (var propertyInfo in BaseDic.PropertyInfo <T>())
            {
                var property     = Expression.Property(Expression.Convert(instance, typeof(T)), propertyInfo.Name);
                var propertyHash = Expression.Constant(propertyInfo.Name.GetHashCode(), typeof(int));
                cases.Add(Expression.SwitchCase(Expression.Convert(property, typeof(object)), propertyHash));
            }

            var switchEx   = Expression.Switch(nameHash, Expression.Constant(null), cases.ToArray());
            var methodBody = Expression.Block(typeof(object), new[] { nameHash }, calHash, switchEx);

            return(Expression.Lambda <Func <object, string, object> >(methodBody, instance, memberName).Compile());
        }
コード例 #2
0
ファイル: BaseMap.cs プロジェクト: jason10wm/FastData.Core
        /// <summary>
        /// 对象映射
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static T CopyModel <T, T1>(T1 model, Expression <Func <T1, object> > field = null) where T : class, new()
        {
            var result = new T();
            var dynGet = new DynamicGet <T1>();
            var dynSet = new DynamicSet <T>();
            var list   = BaseDic.PropertyInfo <T>();
            var dic    = new Dictionary <MemberInfo, Expression>();

            if (field != null)
            {
                var name  = (field.Body as NewExpression).Members.ToList();
                var value = (field.Body as NewExpression).Arguments.ToList();
                for (var i = 0; i < name.Count; i++)
                {
                    dic.Add(name[i], value[i]);
                }
            }

            BaseDic.PropertyInfo <T1>().ForEach(m => {
                if (list.Exists(a => a.Name.ToLower() == m.Name.ToLower()))
                {
                    var property         = list.Find(a => a.Name.ToLower() == m.Name.ToLower());
                    var isList           = m.PropertyType.GetGenericArguments().Length > 0;
                    var isLeafSystemType = isList && m.PropertyType.GetGenericArguments()[0].FullName.StartsWith("System.");
                    var isSystemType     = m.PropertyType.FullName.StartsWith("System.");

                    if (isList && !isLeafSystemType)
                    {
                        var leafList = Activator.CreateInstance(typeof(List <>).MakeGenericType(property.PropertyType.GetGenericArguments()[0]));
                        var tempList = Convert.ChangeType(dynGet.GetValue(model, m.Name, true), m.PropertyType) as IEnumerable;

                        if (tempList != null)
                        {
                            foreach (var temp in tempList)
                            {
                                var leafModel    = Activator.CreateInstance(property.PropertyType.GetGenericArguments()[0]);
                                var propertyList = leafModel.GetType().GetProperties().ToList();

                                temp.GetType().GetProperties().ToList().ForEach(p =>
                                {
                                    if (propertyList.Exists(a => a.Name == p.Name))
                                    {
                                        var tempProperty = propertyList.Find(a => a.Name.ToLower() == p.Name.ToLower());
                                        tempProperty.SetValue(leafModel, p.GetValue(temp));
                                    }
                                });

                                var method = leafList.GetType().GetMethod("Add", BindingFlags.Instance | BindingFlags.Public);
                                method.Invoke(leafList, new object[] { leafModel });
                            }
                            dynSet.SetValue(result, property.Name, leafList, true);
                        }
                    }
                    else if (isSystemType)
                    {
                        if (m.PropertyType.Name == "Nullable`1" && m.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            dynSet.SetValue(result, property.Name, dynGet.GetValue(model, m.Name, true), true);
                        }
                        else
                        {
                            dynSet.SetValue(result, property.Name, Convert.ChangeType(dynGet.GetValue(model, m.Name, true), m.PropertyType), true);
                        }
                    }
                    else
                    {
                        var tempModel    = Convert.ChangeType(dynGet.GetValue(model, m.Name, true), m.PropertyType);
                        var leafModel    = Activator.CreateInstance(property.PropertyType);
                        var propertyList = (property.PropertyType as TypeInfo).GetProperties().ToList();

                        (m.PropertyType as TypeInfo).GetProperties().ToList().ForEach(p =>
                        {
                            if (propertyList.Exists(a => a.Name == p.Name))
                            {
                                var temp = propertyList.Find(a => a.Name.ToLower() == p.Name.ToLower());
                                temp.SetValue(leafModel, p.GetValue(tempModel));
                            }
                        });
                        dynSet.SetValue(result, property.Name, leafModel, true);
                    }
                }
                else
                {
                    if (dic.ToList().Exists(n => (n.Value as MemberExpression).Member.Name.ToLower() == m.Name.ToLower()))
                    {
                        var temp = dic.ToList().Find(n => (n.Value as MemberExpression).Member.Name.ToLower() == m.Name.ToLower());
                        if (m.Name == "Nullable`1" && temp.Key.DeclaringType.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            dynSet.SetValue(result, temp.Key.Name, dynGet.GetValue(model, (temp.Value as MemberExpression).Member.Name, true), true);
                        }
                        else
                        {
                            dynSet.SetValue(result, temp.Key.Name, Convert.ChangeType(dynGet.GetValue(model, (temp.Value as MemberExpression).Member.Name, true), m.PropertyType), true);
                        }
                    }
                }
            });
            return(result);
        }