日期:2014-05-19  浏览次数:20718 次

小弟刚学习C#,请各们高手帮小弟做这一题!
为Car类编写个构造函数,它接受另一个Car对象作为参数,并复制这个参数的所有属性!

------解决方案--------------------
/// <summary>
/// Car の概要の説明です
/// </summary>
public class Car
{
private System.Drawing.Color color;
private int height;
private int width;

public Car(Car car)
{
//
// TODO: コンストラクタ ロジックをここに追加します
//
this.color = car.color;
this.height = car.height;
this.width = car.width;
}
}
------解决方案--------------------
//复制属性和字段
public class car
{
public string aa = null;
car(car c)
{
foreach (System.Reflection.FieldInfo fi in typeof(car).GetFields())
{
fi.SetValue(this,fi.GetValue(c));
}

foreach (System.Reflection.PropertyInfo fi in typeof(car).GetProperties())
{
fi.SetValue(this,fi.GetValue(c));
}
}
}