将对象复制到另一个对象但删除某些属性Windows Phone 8

时间:2013-03-28 12:26:52

标签: c# object copy windows-phone-8 clone

我想将对象复制到另一个对象但删除某些属性。例如

public  class A
{
    public bool IsResizeCancel { get; set; }
    public double MaxSliderValue { get; set; }
    public double CurrentWidth { get; private set; }
    public double CurrentHeight { get; private set; }
}

将对象A复制到对象B,但删除CurrentWidth和CurrentHeight属性

public class B
{
    public bool IsResizeCancel { get; set; }
    public double MaxSliderValue { get; set; }
}

如何使用最少量的代码有效地完成此操作?

1 个答案:

答案 0 :(得分:7)

public class B
{
    public B(A a)
    {
        IsResizeCancel = a.IsResizeCancel;
        MaxSliderValue = a.MaxSliderValue;
    }
    public bool IsResizeCancel { get; set; }
    public double MaxSliderValue { get; set; }
}