1
我想將一個簡單的KeyValuePair對象集合映射到我的自定義類。 可惜的是我只得到一個異常AutoMapper - 映射不起作用
Missing type map configuration or unsupported mapping.
Mapping types:
RuntimeType -> DictionaryType
System.RuntimeType -> AutoMapperTest.Program+DictionaryType
Destination path:
IEnumerable`1[0].Type.Type
Source value:
System.Collections.Generic.KeyValuePair`2[AutoMapperTest.Program+DictionaryType,System.String]
碼在最簡單的形式重現此問題
class Program
{
public enum DictionaryType
{
Type1,
Type2
}
public class DictionariesListViewModels : BaseViewModel
{
public string Name { set; get; }
public DictionaryType Type { set; get; }
}
public class BaseViewModel
{
public int Id { set; get; }
}
static void Main(string[] args)
{
AutoMapper.Mapper.CreateMap<
KeyValuePair<DictionaryType, string>, DictionariesListViewModels>()
.ConstructUsing(r =>
{
var keyValuePair = (KeyValuePair<DictionaryType, string>)r.SourceValue;
return new DictionariesListViewModels
{
Type = keyValuePair.Key,
Name = keyValuePair.Value
};
});
List<KeyValuePair<DictionaryType, string>> collection =
new List<KeyValuePair<DictionaryType, string>>
{
new KeyValuePair<DictionaryType, string>(DictionaryType.Type1, "Position1"),
new KeyValuePair<DictionaryType, string>(DictionaryType.Type2, "Position2")
};
var mappedCollection = AutoMapper.Mapper.Map<IEnumerable<DictionariesListViewModels>>(collection);
Console.ReadLine();
}
}
我有其他的映射創建以同樣的方式(不枚舉)和他們的作品,所以它必須是一個問題,但如何解決它呢?這一定很簡單,我有問題需要注意。