将枚举映射到具有相同枚举类型的属性的类

发布于 2024-11-29 16:16:38 字数 1459 浏览 0 评论 0原文

假设我有一个具有以下定义的类:

public class DestinationOuter
{
    public string Name { get; set; }
    public int Age { get; set; }
    public List<DestinationInner> Siblings { get; set; }
}

public class DestinationInner
{
    public string Name { get; set; }
    public RelationEnum Relation { get; set; }
}

并假设我有一个源类型:

public class SourceSiblings
{
    public string Name { get; set; }
    public RelationEnum Relation { get; set; }
}

使用 AutoMapper,我可以轻松创建一个从 SourceSiblings 映射到 DestinationInner 的配置,这让我像这样进行映射:

SourceSiblings[] brothers = { ... };
DestinationOuter dest = new DestinationOuter();

Mapper.Map(brothers, dest.Siblings);

但我希望能够做的是直接从 SourceSiblings 映射到 DestinationOuter。在这种情况下,DestinationOuter 中的 Name 和 Age 属性将在映射中被忽略,但其想法是 SourceSiblings 将映射到 DestinationOuter.Siblings.使用上面的对象声明,我希望能够做到:

Mapper.Map(brothers, dest);

我不知道如何让它工作。我可以像这样设置配置:

CreateMap<IEnumerable<SourceSiblings>, DestinationOuter>();

但这没有任何作用。看来我需要能够这样说:

CreateMap<IEnumerable<SourceSiblings>, DestinationOuter>()
       .ForMember(dest => dest.Siblings,
                  opt => opt.MapFrom(src => src));

虽然上面的代码编译了,但 Mapper.Map 实际上并不映射值。

Say I have a class with the following definition:

public class DestinationOuter
{
    public string Name { get; set; }
    public int Age { get; set; }
    public List<DestinationInner> Siblings { get; set; }
}

public class DestinationInner
{
    public string Name { get; set; }
    public RelationEnum Relation { get; set; }
}

And say I have a source type:

public class SourceSiblings
{
    public string Name { get; set; }
    public RelationEnum Relation { get; set; }
}

With AutoMapper I can easily create a configuration that maps from SourceSiblings to DestinationInner, which let's me do a mapping like so:

SourceSiblings[] brothers = { ... };
DestinationOuter dest = new DestinationOuter();

Mapper.Map(brothers, dest.Siblings);

But what I'd like to be able to do is map directly from SourceSiblings to DestinationOuter. In this case, the Name and Age properties in DestinationOuter would be ignored in the mapping, but the idea is that SourceSiblings would be mapped onto DestinationOuter.Siblings. Using the object declarations above, I'd like to be able to do:

Mapper.Map(brothers, dest);

I'm not sure how to get this to work. I can setup the configuration like so:

CreateMap<IEnumerable<SourceSiblings>, DestinationOuter>();

But that doesn't do anything. It seems like I need to be able to say something like:

CreateMap<IEnumerable<SourceSiblings>, DestinationOuter>()
       .ForMember(dest => dest.Siblings,
                  opt => opt.MapFrom(src => src));

And while the above compiles, Mapper.Map does not actually map the values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不一样的天空 2024-12-06 16:16:38

这段代码似乎对我有用,但你所说的几乎没有任何作用。

internal class Program
{
    private static void Main(string[] args)
    {
        SourceSiblings[] brothers = {
                                        new SourceSiblings {Name = "A", Relation = 1},
                                        new SourceSiblings {Name = "B", Relation = 2}
                                    };
        var dest = new DestinationOuter();

        Mapper.CreateMap<SourceSiblings, DestinationInner>();

        Mapper.CreateMap<IEnumerable<SourceSiblings>, DestinationOuter>()
            .ForMember(d => d.Name, opt => opt.Ignore())
            .ForMember(d => d.Age, opt => opt.Ignore())
            .ForMember(d => d.Siblings, opt => opt.MapFrom(s => s));

        Mapper.Map(brothers, dest);
        Console.Write(dest.Siblings.Count);
        Console.ReadLine();
    }
}

public class DestinationOuter
{
    public string Name { get; set; }
    public int Age { get; set; }
    public List<DestinationInner> Siblings { get; set; }
}

public class DestinationInner
{
    public string Name { get; set; }
    public int Relation { get; set; }
}

public class SourceSiblings
{
    public string Name { get; set; }
    public int Relation { get; set; }
}

This code seems to work for me, but it's pretty much what you said doesn't do anything.

internal class Program
{
    private static void Main(string[] args)
    {
        SourceSiblings[] brothers = {
                                        new SourceSiblings {Name = "A", Relation = 1},
                                        new SourceSiblings {Name = "B", Relation = 2}
                                    };
        var dest = new DestinationOuter();

        Mapper.CreateMap<SourceSiblings, DestinationInner>();

        Mapper.CreateMap<IEnumerable<SourceSiblings>, DestinationOuter>()
            .ForMember(d => d.Name, opt => opt.Ignore())
            .ForMember(d => d.Age, opt => opt.Ignore())
            .ForMember(d => d.Siblings, opt => opt.MapFrom(s => s));

        Mapper.Map(brothers, dest);
        Console.Write(dest.Siblings.Count);
        Console.ReadLine();
    }
}

public class DestinationOuter
{
    public string Name { get; set; }
    public int Age { get; set; }
    public List<DestinationInner> Siblings { get; set; }
}

public class DestinationInner
{
    public string Name { get; set; }
    public int Relation { get; set; }
}

public class SourceSiblings
{
    public string Name { get; set; }
    public int Relation { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文