Свойства отображения Automapper Not Mapping - PullRequest
0 голосов
/ 13 февраля 2020

Я работал над Generics, и я застрял в странной проблеме. У меня проблема с отображением AutoMapper-9. Я реализовал ICollection, но Automapper неправильно отображает свойства. Мой код

Класс CollectionBase, который реализует ICollection

public class CollectionBase<T> : ICollection<T>
{
    ICollection<T> _items;
    public CollectionBase()
    {
        _items = new List<T>();
    }
    protected CollectionBase(ICollection<T> collection)
    {
        // Let derived classes specify the exact type of ICollection<T> to wrap.
        _items = collection;
    }
    public string FullName { get; set; }
    public bool Enabled { get; set; }
    public int Count
    {
        get
        {
            return _items.Count;
        }
    }

    public bool IsReadOnly
    {
        get
        {
            return false;
        }
    }

    public void Add(T item)
    {
        _items.Add(item);
    }

    public void Clear()
    {
        _items.Clear();
    }

    public bool Contains(T item)
    {
        return _items.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        _items.CopyTo(array, arrayIndex);
    }

    public IEnumerator<T> GetEnumerator()
    {
        return _items.GetEnumerator();
    }

    public bool Remove(T item)
    {
        return _items.Remove(item);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return _items.GetEnumerator();
    }
}

ChildrenElements

public class ChildrenElements
{
    public string SchoolName { get; set; }
    public string CityName { get; set; }
}

ChildrenCollection -> Унаследовано от CollectionBase

public class ChildrenCollection : CollectionBase<ChildrenElements>
{
    public ChildrenCollection()
    {
    }
    public int DefaultValue { get; set; }
}

Детский класс

public class Children : IChildren
{
    public Children()
    {
    }
    public Children(IChildren children, bool flag = true)
    {
        Name = "Test";
        FatherName = "TestFather";
        childrenCollection = new ChildrenCollection() { DefaultValue = -1, FullName = "Children Name 1", Enabled = false };
        if (children != null)
        {
                /////////////////////////////////////////////////////////////////////
                ////            Using Generic Mapper Configuration                ///
                /////////////////////////////////////////////////////////////////////
                var mapper = ObjectMapperFactory.CreateMapper<IChildren, IChildren>();
                mapper.Map(children, this);

        }
    }
    public string Name { get; set; }
    public string FatherName { get; set; }
    public ChildrenCollection childrenCollection { get; set; }
}

Фабрика картографирования объектов для AutoMapper

public static class ObjectMapperFactory
{
    public static IMapper CreateMapper<TSource, TDestination>()
    {
        var config = new MapperConfiguration(
            cfg =>
            {
                cfg.CreateMap<TSource, TDestination>()
                .PreserveReferences()
                cfg.Advanced.AllowAdditiveTypeMapCreation = true;
                cfg.AllowNullDestinationValues = true;
                cfg.AllowNullCollections = true;
            });

        config.AssertConfigurationIsValid();
        return config.CreateMapper();
    }
}

Mapper не отображает свойства в классе "ChildrenCollection". Он отображает «ChildrenElements» правильно, но свойства класса ChildrenCollection не отображаются правильно, как DefaultDays, Enabled и т. Д. c.

Заранее спасибо.

...