private void CompareObject(object parentObj, object childObj)
        {
            Type t1 
= parentObj.GetType();//得到父类的类型
            Type t2 = childObj.GetType(); //得到子类的类型
            foreach (PropertyInfo p1 in t1.GetProperties())
            {
                
foreach (PropertyInfo p2 in t2.GetProperties())
                {
                    
if (p1.PropertyType == p2.PropertyType && p1.Name == p2.Name)
                    {
                        p2.SetValue(childObj, p1.GetValue(parentObj, 
null), null);//给子类对象赋值
                        break;
                    }
                }
            }
        } 

 

 

子类得到父类和子类都共有属性的值,后续的操作可以给子类的其他属性赋值

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2021-10-23
猜你喜欢
  • 2021-07-27
  • 2021-11-06
  • 2021-06-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-02
相关资源
相似解决方案