简体   繁体   English

在同一对象中映射嵌套属性

[英]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. 我想将Bar类(位于de Foo类中)的属性映射到Bar类上的属性。 So I've an instance of Foo and SomeIntProperty and SomeString property has their default value, the Bar property instead has some real values. 因此,我有一个Foo和SomeIntProperty实例,并且SomeString属性具有其默认值,而Bar属性则具有一些实际值。 I want to map those values from the Bar property from Foo to Foo. 我想将Bar属性中的值从Foo映射到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:无需创建新对象即可更新属性值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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