作者a210s02 (虫虫)
看板C_Sharp
标题Re: [问题] 如何把父类别的资料复制到子类别?
时间Thu Sep 8 15:44:23 2011
※ 引述《apiod ( )》之铭言:
: public class A
: {
: public string FirstName { get; set; }
: public string LastName { get; set; }
: public string Address { get; set; }
: }
: public class B : A
: {
: public string company { get;set;}
/*新增这边*/
public B(A x)
{
this.FirstName = x.FisrstName;
this.SecondName = x.SecondName;
this.Address = x.Address;
}
: }
: A a = new A();
: a.FirstName = "xx1";
: a.LastName = "xx2";
: a.Address = "xx3";
//方法1需要新增上面的部分
B b = new B(a);
//方法2
B b = new B();
b.FisrstName = a.FistName;
b.SecondName = a.SecondName;
b.Address = a.Address;
: 请问我该如何把A的资料复制给B呢?
: 感谢解惑
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.122.36.38
1F:→ apiod:我的类别和属性都非常多 这样一个一个写会很累 09/08 15:49
2F:→ apiod:我是想知道有没有比较快速的方法 09/08 15:49
3F:→ kinwind:用Reflection 09/08 17:08
4F:→ hanyan:不要用继承,使B里面有个ref A如何? 09/08 19:18
5F:→ a210s02:用List呢? 09/08 19:35
6F:→ yeo1987:直接ref传过去真的比较简单- - 09/08 20:53
7F:→ suching:能不能假装B是A, 然後反序列化地接受A序列化後的资料? 09/15 04:41
8F:→ ssccg:在constructor用Reflection回圈比对Property的名字填资料 09/18 00:06