Automapper 自动映射错误:尚未支持从方法映射的表达式
知道什么会导致错误表达式映射到不受支持的方法吗。当尝试映射两个对象时?我在任何地方都找不到有关此错误的任何引用 编辑-- 我有更多的信息。我的DTO中有一个财产声明为:Automapper 自动映射错误:尚未支持从方法映射的表达式,automapper,Automapper,知道什么会导致错误表达式映射到不受支持的方法吗。当尝试映射两个对象时?我在任何地方都找不到有关此错误的任何引用 编辑-- 我有更多的信息。我的DTO中有一个财产声明为: public LookupItem RegionType { get; set; } 但是,当我调用映射时,它会生成错误,表达式映射来自尚未受支持的方法 但是,如果我将属性名类型中的字符串更改为任何其他类型,如Typeo或ASDF,则映射会成功。换句话说,如果将属性名称更改为RegionTypeo。我在这里有没有违反惯例?在
public LookupItem RegionType { get; set; }
但是,当我调用映射时,它会生成错误,表达式映射来自尚未受支持的方法
但是,如果我将属性名类型中的字符串更改为任何其他类型,如Typeo或ASDF,则映射会成功。换句话说,如果将属性名称更改为RegionTypeo。我在这里有没有违反惯例?在我的属性名中包含字符串类型似乎有问题
以下是生成的错误:
结果消息:
Test method Rep.Tests.PlanServiceTest.GetBuildings threw exception:
System.NotImplementedException: Expressions mapping from methods not supported yet.
Result StackTrace:
at AutoMapper.PropertyMap.ResolveExpression(Type currentType, Expression instanceParameter)
at AutoMapper.QueryableExtensions.Extensions.CreateMemberBindings(IMappingEngine mappingEngine, Type typeIn, TypeMap typeMap, Expression instanceParameter)
at AutoMapper.QueryableExtensions.Extensions.CreateMapExpression(IMappingEngine mappingEngine, Type typeIn, Type typeOut, Expression instanceParameter)
at AutoMapper.QueryableExtensions.Extensions.CreateMapExpression(IMappingEngine mappingEngine, Type typeIn, Type typeOut)
at AutoMapper.QueryableExtensions.Extensions.<>c__DisplayClass12.<CreateMapExpression>b__0(TypePair tp)
at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)
at AutoMapper.Internal.DictionaryFactoryOverride.ConcurrentDictionaryImpl2.GetOrAdd(TKey key, Func2 valueFactory)
at AutoMapper.QueryableExtensions.Extensions.CreateMapExpression[TSource,TDestination](IMappingEngine mappingEngine)
at AutoMapper.QueryableExtensions.ProjectionExpression1.ToTResult
at Rep.Services.PlanService.GetBuildings() in c:\Dev\REP\Rep\Services\PlanService.cs:line 369
at Rep.Tests.PlanServiceTest.GetBuildings() in c:\Dev\REP\Rep.Tests\PlanServiceTest.cs:line 50
基于,您可以看到,在尝试对对象映射函数时引发异常:
public ExpressionResolutionResult ResolveExpression(Type currentType, Expression instanceParameter)
{
Expression currentChild = instanceParameter;
Type currentChildType = currentType;
foreach (var resolver in GetSourceValueResolvers())
{
var getter = resolver as IMemberGetter;
if (getter != null)
{
var memberInfo = getter.MemberInfo;
var propertyInfo = memberInfo as PropertyInfo;
if (propertyInfo != null)
{
currentChild = Expression.Property(currentChild, propertyInfo);
currentChildType = propertyInfo.PropertyType;
}
else
{
throw new NotImplementedException("Expressions mapping from methods not supported yet.");
}
}
else
{
var oldParameter = CustomExpression.Parameters.Single();
var newParameter = instanceParameter;
var converter = new ConversionVisitor(newParameter, oldParameter);
currentChild = converter.Visit(CustomExpression.Body);
currentChildType = currentChild.Type;
}
}
return new ExpressionResolutionResult(currentChild, currentChildType);
}
根据OP澄清,我无法再现以下问题:
public class Class1
{
public string StringType { get; set; }
public Func<Class1> FuncType { get; set; }
public Class1 Class1Type { get; set; }
}
public class Class2
{
public string StringType { get; set; }
public Func<Class1> FuncType { get; set; }
public Class1 Class1Type { get; set; }
}
/* ... */
AutoMapper.Mapper.CreateMap<Class1, Class2>();
var c1 = new Class1() { Class1Type = new Class1(), FuncType = () => new Class1(), StringType = "Class1" };
var c2 = AutoMapper.Mapper.Map<Class1, Class2>(new Class1());
我有更多信息,我的DTO中有一个属性声明为:public LookupItem RegionType{get;set;}。但是,当我调用映射时,它会生成错误,表达式映射来自不受支持的方法。。但是,如果我将属性名类型中的字符串更改为任何其他类型,如Typeo或ASDF,则映射会成功。我是否违反了任何惯例规则?@xgp您能将此信息添加到您的问题中,以便从更多的空间中获益吗您的代码是否使用自定义成员解析程序?我不明白属性名称中的字符串是什么意思。您是否也可以为DTO发布您的CreateMap和Map调用?已澄清。将字符串类型i包含在属性名称中似乎有问题。我无法重现此问题,请发布您的CreateMap调用和Map调用。您有机会找出问题所在吗?