2014-03-25 56 views
1

任何想法可能導致錯誤「表達式映射方法尚不支持」。當試圖映射兩個對象?我無法在任何地方找到任何有關此錯誤的參考。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 
+0

您的代碼是否使用自定義成員解析器? – samy

+0

我不明白你的意思是「屬性名稱中的字符串」。你還可以發佈DTO的CreateMap和Map調用嗎?澄清了 – samy

+0

。包含字符串「類型」我屬性名稱似乎有問題。 – xgp

回答

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澄清,我不能再現與fol的問題正在降低:

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

我有更多信息,我在我的DTO中有一個屬性聲明爲: public LookupItem RegionType {get;組; } 但是,當我調用映射時,它會生成錯誤「從尚未支持的方法映射表達式」。 但是,如果將屬性名稱「Type」中的字符串更改爲「Typeo」或「ASDF」之類的任何其他字符,則映射將成功。我在這裏違反任何約定規則嗎? – xgp

+0

@xgp您能否將這些信息添加到您的問題中,以便從更多空間中受益? :) – samy

相關問題