需求 :
有一个保存数据库字段的基础类,现在要加个状态返回给前端,但是又不能改基础类;
class BaseA { public string Name { get; set; } }
class A:BaseA { public A(BaseA baseA) { var thebasea = baseA.GetType().GetProperties(); foreach (var item in thebasea) { item.SetValue(this, item.GetValue(baseA)); } } public string Age { get; set; } }
static void Main(string[] args)
{
BaseA baseA = new BaseA();
baseA.Name = "小明";
A a = new A(baseA);
a.Age = "17岁";
Console.WriteLine(a.Age+a.Name);
}
结果:
来至群193011217
[.Net C#]飘叶
感谢大佬