对象映射错误

本文关键字:错误 映射 对象 | 更新日期: 2023-09-27 18:27:46

WCF 中的第一个模型

public class One
{
    public string A { get; set; }
    public string B { get; set; } 
}

我的第二个

public class Two : One
{
    public string C { get; set; }
}

现在我有了模型二的属性值,比如这个

Two obj = new Two()
{
   A="ww",
   B="WWW",
   C="EE"
};
One obj1 = new One();

现在我想将我的obj对象值复制到obj1。但在复制第一个对象时,我需要跳过第三个值。。怎么做?

对象映射错误

您可以为One:创建一个复制构造函数

public class One
{
    public One(One other)
    {
        A = other.A;
        B = other.B;
    }
    public string A { get; set; }
    public string B { get; set; } 
}

并像这样使用:

Two two = new Two
{
    A="ww",
    B="WWW",
    C="EE"
};
One one = new One(two);