Пример #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
        /// <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 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 => string.Compare(a.Name, m.Name, true) == 0))
                {
                    var property         = list.Find(a => string.Compare(a.Name, m.Name, true) == 0);
                    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(BaseEmit.Get <T1>(model, m.Name), 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 => string.Compare(a.Name, p.Name, true) == 0);
                                        tempProperty.SetValue(leafModel, p.GetValue(temp));
                                    }
                                });

                                var method = leafList.GetType().GetMethod("Add", BindingFlags.Instance | BindingFlags.Public);
                                BaseEmit.Invoke(leafList, method, new object[] { leafModel });
                            }

                            BaseEmit.Set <T>(result, property.Name, leafList);
                        }
                    }
                    else if (isSystemType)
                    {
                        BaseEmit.Set <T>(result, property.Name, BaseEmit.Get <T1>(model, m.Name));
                    }
                    else
                    {
                        var tempModel    = Convert.ChangeType(BaseEmit.Get <T1>(model, m.Name), 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 => string.Compare(a.Name, p.Name, true) == 0);
                                temp.SetValue(leafModel, p.GetValue(tempModel));
                            }
                        });

                        BaseEmit.Set <T>(result, property.Name, leafModel);
                    }
                }
                else
                {
                    if (dic.ToList().Exists(n => string.Compare((n.Value as MemberExpression).Member.Name, m.Name, true) == 0))
                    {
                        var temp = dic.ToList().Find(n => string.Compare((n.Value as MemberExpression).Member.Name, m.Name, true) == 0);
                        BaseEmit.Set <T>(result, temp.Key.Name, BaseEmit.Get <T1>(model, (temp.Value as MemberExpression).Member.Name));
                    }
                }
            });

            return(result);
        }