AutoMapper: InvalidCastException when mapping IReadOnlyDictionary to the same type
Source/destination types
public class Source
{
public IReadOnlyDictionary<int, int> Values { get; set; }
}
public class Destination
{
public IReadOnlyDictionary<int, int> Values { get; set; }
}
Mapping configuration
var mapper = new MapperConfiguration(config =>
{
config.CreateMap<Source, Destination>();
}).CreateMapper();
Version: 7.0.1
Expected behavior
Should successfully map Source.Values
to Destination.Values
.
Actual behavior
Throws AutoMapperMappingException
:
Error mapping types.
Mapping types: Source -> Destination UserQuery+Source -> UserQuery+Destination
Type Map configuration: Source -> Destination UserQuery+Source -> UserQuery+Destination
Property: Values
(UserQuery
is the root class in a LINQPad query)
Inner exception: InvalidCastException
:
Unable to cast object of type ‘System.Collections.Generic.List
1[System.Collections.Generic.KeyValuePair
2[System.Int32,System.Int32]]’ to type ‘System.Collections.Generic.IReadOnlyDictionary`2[System.Int32,System.Int32]’.
Steps to reproduce
var source = new Source
{
Values = new Dictionary<int, int> { [1] = 42 }
};
var destination = mapper.Map<Destination>(source);
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15 (14 by maintainers)
I’m gonna add a new issue for this one, for tracking purposes.
I doubt it. If you’ll give it some thought it should be easy to do.