自动映射错误:表达式映射不支持的方法

时间:2014-03-25 21:46:45

标签: automapper

知道什么可能导致错误“表达式从尚未支持的方法映射”。当试图映射两个对象?我无法在任何地方找到任何对此错误的引用。

EDITED ---

我有更多信息。我的DTO中有一个属性声明为:

 public LookupItem RegionType { get; set; }

但是,当我调用映射时,它会生成错误“表达式从尚不支持的方法映射”。

但是,如果我将属性名称“Type”中的字符串更改为“Typeo”或“ASDF”之类的其他内容,则映射成功。换句话说,如果将属性名称更改为“RegionTypeo”。我在这里违反任何公约规则吗?在我的属性名称中包含字符串“Type”似乎有问题。

以下是生成的错误:

结果讯息:

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

1 个答案:

答案 0 :(得分:0)

基于the source code,您可以看到在对象上尝试映射函数时抛出异常:

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());