简体   繁体   中英

Mapping nested properties in same object

I've a question and I hope this is possible. I want to map properties from an nested class into his parent class (without creating a new object, this should be the same object) Below I've attached an example.

I want to map the properties from the Bar class (which is in de Foo class) to the properties on the Bar class. So I've an instance of Foo and SomeIntProperty and SomeString property has their default value, the Bar property instead has some real values. I want to map those values from the Bar property from Foo to Foo. Is this possible, and is it possible without specifying a mapper for the properties? My classes contains a lot of properties..

I find it really hard to explain, sorry if it is still confusing.

    public class Foo
{
    public int SomeIntProperty { get; set; }
    public string SomeStringProperty { get; set; }

    // the properties from this Bar instance should be mapped to the properties above.
    public Bar Bar { get; set; }
}

public class Bar
{
    public int SomeIntProperty { get; set; }
    public string SomeStringProperty { get; set; }
    public bool ThisPropertyDoesntExistsOnFooClass { get; set; }
}

您可以按照以下说明使用Automapper: Automapper:无需创建新对象即可更新属性值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM