Unable to map property of type custom collection interface #4029
-
Source/destination typespublic class Source
{
public ICustomCollection Collection { get; set; }
}
public class Destination
{
public ICustomCollection Collection { get; set; }
}
public interface ICustomCollection : ICollection<int>
{
}
public class CustomCollection : ICustomCollection
{
private readonly List<int> data = new();
public int Count => data.Count;
public bool IsReadOnly => false;
public void Add(int item) => data.Add(item);
public void Clear() => data.Clear();
public bool Contains(int item) => data.Contains(item);
public void CopyTo(int[] array, int arrayIndex) => data.CopyTo(array, arrayIndex);
public IEnumerator<int> GetEnumerator() => data.GetEnumerator();
public bool Remove(int item) => data.Remove(item);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
} Mapping configurationCreateMap<Source, Destination>(); Version: 11.0.1Expected behaviorProperty Actual behaviorA mapping exception was thrown Steps to reproducepublic static class Program
{
public static void Main()
{
var mapper = new MapperConfiguration(c => c.CreateMap<Source, Destination>()).CreateMapper();
var source = new Source()
{
Collection = new CustomCollection() { 1, 2, 0 }
};
// the following line throws a mapping exception "Unable to cast object of type 'System.Collections.Generic.List`1[System.Int32]' to type 'Test.ICustomCollection'."
var destination = mapper.Map<Destination>(source);
}
} Link to fiddle |
Beta Was this translation helpful? Give feedback.
Answered by
lbargaoanu
Jul 26, 2022
Replies: 1 comment 4 replies
-
Check the execution plan. |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
lbargaoanu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check the execution plan.