Net Core教程

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

本文主要是介绍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 });

            }

        }

这篇关于C#反射--基于反射的拷贝函数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!