码迷,mamicode.com
首页 > 其他好文 > 详细

反射,自定义特性,对象复制

时间:2017-10-29 23:20:16      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:each   col   console   break   判断   mat   etc   bsp   world   

 [AttributeUsage(AttributeTargets.All)]
    public class VersionAttribute : Attribute
    {
        public string Name { get; set; }
        public string Date { get; set; }
        public string Describtion { get; set; }
    }

    [Version(Date = "2017 - 10 - 29", Describtion = "hello world", Name = "wjire")]
    public class MyCode
    {
        [Version(Name = "GId")]
        public int Id { get; set; }

        [Version(Name = "GName")]
        public string Name { get; set; }

     public int Age { get; set; } }
public class GCode { public string GWjire { get; set; } public int GId { get; set; } public string GName { get; set; } } public class Code { public int Id { get; set; } public string Name { get; set; } }

 

            var customAttr = (VersionAttribute)Attribute.GetCustomAttribute(typeof(MyCode), typeof(VersionAttribute));
            string name = customAttr.Name;
            string date = customAttr.Date;
            string desc = customAttr.Describtion;
            Console.WriteLine($"name={name},date={date},desc={desc}");

 

属性名称相同的两个类之间的复制

            MyCode myCode = new MyCode()
            {
                Name = "refuge",
                Id = 1
            };
            Code code = new Code();
            var myCodePro = typeof(MyCode).GetProperties();
            var codePro = typeof(Code).GetProperties();
            foreach (var mp in myCodePro)
            {
                foreach (var p in codePro)
                {
                    if (p.Name == mp.Name)
                    {
                        p.SetValue(code, mp.GetValue(myCode));
                        break;
                    }
                }
            }

            Console.WriteLine($"code.Name={code.Name}");
            Console.WriteLine($"code.Id={code.Id}");

 

属性名称不相同的两个类之间的复制

            MyCode myCode = new MyCode()
            {
                Name = "refuge",
                Id = 1
            };
            GCode gCode = new GCode();
            var myCodePro = typeof(MyCode).GetProperties();
            var gCodePro = typeof(GCode).GetProperties();
            foreach (var mp in myCodePro)
            {
                var c = (VersionAttribute)Attribute.GetCustomAttribute(mp, typeof(VersionAttribute));

                //MyCode类的 Age 属性没有特性,所以这里要加判断,不然会报异常
                string name = c == null ? string.Empty : c.Name;

                foreach (var gp in gCodePro)
                {
                    if (gp.Name == name)
                    {
                        gp.SetValue(gCode, mp.GetValue(myCode));
                        break;
                    }
                }
            }

 

反射,自定义特性,对象复制

标签:each   col   console   break   判断   mat   etc   bsp   world   

原文地址:http://www.cnblogs.com/refuge/p/7751369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!