zoukankan      html  css  js  c++  java
  • 反射PropertyInfo的简单使用

    namespace EF6._0Test
    {
        class Program
        {
            /// <summary>
            /// PropertyInfo的简单使用
            /// </summary>
            static void Main(string[] args)
            {
                Student stu1 = new Student() { id = 1, name = "张三", length = 175, datetime = DateTime.Now };
                Student1 stu2 = new Student1() { id = 2, name = "李四", length = 176, datetime = DateTime.Now.AddDays(1) };
                var list1 = stu1.GetType().GetProperties();
                var list2 = stu2.GetType().GetProperties();
                //stu1 mapping stu2
                foreach (var item in list1)
                {
                    var entityPi = list2.FirstOrDefault(p => p.Name == item.Name);
                    entityPi.SetValue(stu2, item.GetValue(stu1));
                }
                Console.WriteLine($"id={stu2.id}  ,name={stu2.name}  ,length={stu2.length}  ,datetime={stu2.datetime}");
                Console.ReadLine();
            }
        }
    
        public class Student
        {
            public int id { get; set; }
            public string name { get; set; }
            public decimal length { get; set; }
            public DateTime datetime { get; set; }
        }
    
        public class Student1
        {
            public int id { get; set; }
            public string name { get; set; }
            public decimal length { get; set; }
            public DateTime datetime { get; set; }
        }
    }

  • 相关阅读:
    Task Schedule
    Number Game
    CDQ分治
    Friends and Subsequences
    HDU5266 pog loves szh III
    P1593 因子和
    求一个数的欧拉函数的优化
    Grandpa's Estate POJ
    LightOJ
    Paint The Wall HDU
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/7879588.html
Copyright © 2011-2022 走看看