简体   繁体   English

AutoMapper为什么不映射这些嵌套的集合?

[英]Why doesn't AutoMapper map these nested collections?

I have the following source classes in VB. 我在VB中具有以下源类。

Public Class Product
...

    Public Property itemizedSize As Size()
        Get
            Return _arrItemizedSizes
        End Get
        Set(ByVal value As Size())
            _arrItemizedSizes = value
        End Set
    End Property
...
End Class


Public Class Size
    Private _strName As String
    Private _intQuantity As Integer
    Private _intScale As Integer

    Public Property name() As String
        Get
            Return _strName
        End Get
        Set(ByVal value As String)
            _strName = value
        End Set
    End Property

    Public Property quantity() As Integer
        Get
            Return _intQuantity
        End Get
        Set(ByVal value As Integer)
            _intQuantity = value
        End Set
    End Property

    Public Property Scale() As Integer
        Get
            Return _intScale
        End Get
        Set(ByVal value As Integer)
            _intScale = value
        End Set
    End Property

    Public Sub New()
        _strName = ""
        _intQuantity = 0
        _intScale = 0
    End Sub

    Public Sub New(ByVal name As String, ByVal quantity As Integer, Optional ByVal Scale As Integer = 0)
        _strName = name
        _intQuantity = quantity
        _intScale = Scale
    End Sub
End Class

and I am trying to map it to these C# classes 我正在尝试将其映射到这些C#类

public class ProductsViewModel : List<ProductViewModel>
{
    ...
}

public class ProductViewModel
{
    ...
    public SizeViewModel[] ItemizedSize { get; set; }
    ...
}

public class SizeViewModel
{
    public string Name { get; set; }
    public int Quantity { get; set; }
}

I am using the following code to do my mapping... however, I get an exception saying that a mapping is not defined from Size to SizeViewModel 我正在使用以下代码进行映射...但是,我收到一个异常,说未定义从Size到SizeViewModel的映射

AutoMapper.Mapper.CreateMap<Size, SizeViewModel>();
AutoMapper.Mapper.CreateMap<Product, ProductViewModel>();

ProductsViewModel model = AutoMapper.Mapper.Map<List<Product>, ProductsViewModel>(productDetails);
AutoMapper.Mapper.AssertConfigurationIsValid();

Is there something that I am missing? 有什么我想念的吗? Any help would be much appreciated.. thanks! 任何帮助将不胜感激..谢谢!

I just ran your code and everything works perfectly for me. 我只运行了您的代码,一切对我来说都很完美。 What version of AutoMapper are you using? 您正在使用哪个版本的AutoMapper? I'm using version 2.0.0.0. 我正在使用2.0.0.0版。

Here is my VB code: 这里是我的VB代码:

Public Class Product


    Public Property itemizedSize As Size()
        Get
            Return _arrItemizedSizes
        End Get
        Set(ByVal value As Size())
            _arrItemizedSizes = value
        End Set
    End Property

    Private Property _arrItemizedSizes As Size()

End Class


Public Class Size
    Private _strName As String
    Private _intQuantity As Integer
    Private _intScale As Integer

    Public Property name() As String
        Get
            Return _strName
        End Get
        Set(ByVal value As String)
            _strName = value
        End Set
    End Property

    Public Property quantity() As Integer
        Get
            Return _intQuantity
        End Get
        Set(ByVal value As Integer)
            _intQuantity = value
        End Set
    End Property

    Public Property Scale() As Integer
        Get
            Return _intScale
        End Get
        Set(ByVal value As Integer)
            _intScale = value
        End Set
    End Property

    Public Sub New()
        _strName = ""
        _intQuantity = 0
        _intScale = 0
    End Sub

    Public Sub New(ByVal name As String, ByVal quantity As Integer, Optional ByVal Scale As Integer = 0)
        _strName = name
        _intQuantity = quantity
        _intScale = Scale
    End Sub
End Class

Here is the C#: 这是C#:

using System;
using System.Collections.Generic;
using SimpleLib;  //That is the VB assembly

namespace ConsoleApplication1
{
    public class ProductsViewModel : List<ProductViewModel>
    {

    }

    public class ProductViewModel
    {
        public SizeViewModel[] ItemizedSize { get; set; }
    }

    public class SizeViewModel
    {
        public string Name { get; set; }
        public int Quantity { get; set; }

        public override string ToString()
        {
            return this.Name + " " + this.Quantity.ToString();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            AutoMapper.Mapper.CreateMap<Size, SizeViewModel>();
            AutoMapper.Mapper.CreateMap<Product, ProductViewModel>();

            List<Product> productDetails = new List<Product>()
                                               {
                                                   new Product() {itemizedSize = new Size[1] {new Size("hello", 2, 5)}},
                                                   new Product() {itemizedSize = new Size[1] {new Size("hello2", 4, 10)}}
                                               };

            ProductsViewModel model = AutoMapper.Mapper.Map<List<Product>, ProductsViewModel>(productDetails);
            AutoMapper.Mapper.AssertConfigurationIsValid();

            Console.WriteLine("Count: {0}", model.Count);
            Console.WriteLine("First Product: {0}", model[0].ItemizedSize[0].ToString());
            Console.WriteLine("Second Product: {0}", model[1].ItemizedSize[0].ToString());

            Console.ReadLine();
        }
    }
}

As you can see, I'm using a console app to test it. 如您所见,我正在使用控制台应用程序对其进行测试。 I get everything mapping through correctly and no Configuration errors from AutoMapper. 我正确地获得了所有映射,并且AutoMapper没有配置错误。

This is the output I get: 这是我得到的输出:

Count: 2
First Product: hello 2
Second Product: hello2 4

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

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