C#反射--基于反射的拷贝函数

 

        public void SetMaterial(Material model)

        {

            Type t = model.GetType();

            PropertyInfo[] fieldInfos = t.GetProperties();

            List<string> defaultFields = new List<string>()

            {

                //"IsDeleted",

                //"DeleterId",

                //"DeletionTime",

                "LastModificationTime",

                "LastModifierId",

                "LastModifier",

                "CreationTime",

                "CreatorId",

                "Creator",

                "ApproveTime",

                "ApproverId",

                "ExtraProperties",

                "UnApproveTime",

                "UnApproverId",

                "CloseTime",

                "CloserId",

                "UnCloseTime",

                "UnCloserId",

                "ConcurrencyStamp",

                "Id",

                "TextureModel",

                "BrandModel",

                "Unit",

                "Category",

                "ChargeUnit",

                "MultiCompany",

            };

            foreach (var field in fieldInfos)

            {

                if (defaultFields.Contains(field.Name))

                    continue;

                //调用InvokeMember提取当前属性值

                object myvalue = t.InvokeMember(field.Name, BindingFlags.GetProperty, null, model, null);

                //将提取出来的属性值赋值到当前对象

                t.InvokeMember(field.Name, BindingFlags.SetProperty, null, this, new object[] { myvalue });

            }

        }

上一篇:F7操作前校验


下一篇:day39 redis 数据类型 String 字符串、List 列表、Hash 散列