C# 拷贝实体

 public static T CloneEntity<T>(T entity) where T:new()
        {
            T ent = new T();
            PropertyInfo[] propertyInfoes = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo propertyInfo in propertyInfoes)
            {

               
                if (propertyInfo.PropertyType.IsArray || (propertyInfo.PropertyType.IsClass && propertyInfo.PropertyType != typeof(string)))
                {
                    object child = propertyInfo.GetValue(entity,null);
                    if (child == null)
                        continue;
                    Type childType = child.GetType();
                    if (childType.IsGenericType)
                    {
                     
                     Type typeDefinition=   childType.GetGenericArguments()[0];
                     IList items = childType.Assembly.CreateInstance(childType.FullName) as IList ;
                      
                       PropertyInfo[] childPropertyInfoes = typeDefinition.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                        IList lst=child as IList;
                        for(int i=0;i<lst.Count;i++)
                        {
                            object itemEntity = null;
                            if ( typeDefinition.IsClass && typeDefinition != typeof(string))
                            {
                                itemEntity = typeDefinition.Assembly.CreateInstance(typeDefinition.FullName);
                                foreach (PropertyInfo childProperty in childPropertyInfoes)
                                {
                                    childProperty.SetValue(itemEntity, childProperty.GetValue(lst[i], null), null);
                                }
                            }
                            else
                            {
                                itemEntity = lst[i];
                            }
                 
                          
                            items.Add(itemEntity);
                        }
                        propertyInfo.SetValue(ent, items, null);

                    }
                    continue;
                }
                propertyInfo.SetValue(ent, propertyInfo.GetValue(entity, null), null);
            }

            FieldInfo[] fieldInfoes = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance);
            foreach (FieldInfo fieldInfo in fieldInfoes)
            {
                fieldInfo.SetValue(ent, fieldInfo.GetValue(entity));
            }

            return ent;
        }
     
    }

你可能感兴趣的:(泛型,拷贝实体)