Can Expressmapper copy to destination?

别来无恙 提交于 2020-01-06 02:20:30

问题


I'm currently using expressmapper.

And I'm facing an issue, when i try co map an object to another one (aka destination). I get NullReferenceException, what seems to happen is when object "source" has some complexe properties (non value type) and "destination" haven't initialized them (ie: destination.MyListOfThing = null) then expressmapper throw the exception. Also when working with IList, elements don't get copied.

I'm not sure it's a "by design" behaviour, because a versatile mapping framework is one where you don't have to know anything (except from the type maybe) of the source object.

Is this a bug or something you can configure?

Edit: here are my classes definitions:

    public class MapSource
    {
        public int Id { get; set; }
        public Guid Guid { get; set; }
        public MapSource Parent { get; set; }
        public List<MapSource> Children { get; set; }

        public static implicit operator int(MapSource src)
        {
            return src.Id;
        }
    }

    public class MapDestination
    {
        public int Id { get; set; }
        public Guid Guid { get; set; }
        public MapDestination Parent { get; set; }
        public List<MapSource> Children { get; set; }
    }

A script to populate one:

    public static IEnumerable<object[]> Source
    {
        get
        {
            var src = new MapSource
            {
                Id = Guid.NewGuid().GetHashCode(),
                Guid = Guid.NewGuid(),
                Parent = new MapSource
                {
                    Id = Guid.NewGuid().GetHashCode(),
                    Guid = Guid.NewGuid()
                }
            };
            src.Children = Enumerable.Range(0, 3).Select(i => new MapSource
            {
                Id = Guid.NewGuid().GetHashCode(),
                Guid = Guid.NewGuid()
            }).ToList();

            return new [] { new object[] { src }};
        }
    }

来源:https://stackoverflow.com/questions/36545384/can-expressmapper-copy-to-destination

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!