2016-10-17 16 views
0

コード:automapperで「タイプマップ設定が見つからないか、サポートされていないマッピングが見つかりません」というエラーが表示されるのはなぜですか?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using AutoMapper; 

namespace TestAutomapper 
{ 
    class Program 
    { 
    static void Main(string[] args) 
    { 

     var config = new MapperConfiguration(cfg => cfg.CreateMap<MyClassSource, MyClassDestination>()); 


     var mapper = config.CreateMapper(); 

     var source = new MyClassSource { DateTimeValue = null }; 

     var mapped = mapper.Map<MyClassSource, MyClassDestination>(source); 
    } 

    } 

    public class MyClassSource 
    { 
    public object DateTimeValue { get; set; } 
    } 

    public class MyClassDestination 
    { 
    public DateTime? DateTimeValue { get; set; } 
    } 
} 

エラーは次のとおりです。

AutoMapper.AutoMapperMappingException was unhandled 
     HResult=-2146233088 
     Message=Error mapping types. 

    Mapping types: 
    MyClassSource -> MyClassDestination 
    TestAutomapper.MyClassSource -> TestAutomapper.MyClassDestination 

    Type Map configuration: 
    MyClassSource -> MyClassDestination 
    TestAutomapper.MyClassSource -> TestAutomapper.MyClassDestination 

    Property: 
    DateTimeValue 
     Source=Anonymously Hosted DynamicMethods Assembly 
     StackTrace: 
      at lambda_method(Closure , MyClassSource , MyClassDestination , ResolutionContext) 
      at TestAutomapper.Program.Main(String[] args) in C:\Users\costa\documents\visual studio 2015\Projects\TestAutomapper\TestAutomapper\Program.cs:line 22 
      at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
      at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
      at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
      at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
      at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
      at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
      at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
      at System.Threading.ThreadHelper.ThreadStart() 
     InnerException: 
      HResult=-2146233088 
      Message=Missing type map configuration or unsupported mapping. 

    Mapping types: 
    Object -> Nullable`1 
    System.Object -> System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] 
      Source=Anonymously Hosted DynamicMethods Assembly 
      StackTrace: 
       at lambda_method(Closure , Object , Nullable`1 , ResolutionContext) 
       at lambda_method(Closure , MyClassSource , MyClassDestination , ResolutionContext) 
      InnerException: 

私は(https://github.com/AutoMapper/AutoMapper/issues/1095)このエラーが解決したと思いました。私はAutomapper 5.1.1を使用しています。

これを修正するにはどうすればよいですか?

おかげ

編集:だけを明確にするには、私は、NULL値の取り扱いと心配です。私は非nullオブジェクト値からDateTimeへの変換が複雑であることを理解しています。実際のコードでは、ソースオブジェクトの実際の値はnullまたはDateTimeです。私はnullがエラーなしで処理されると思った。

編集:

私が今までにオブジェクトを変換するために、拡張メソッドのToDateを作成したと私はオブジェクトからのDateTimeに変換を処理するために、このマッピングを追加した?:あなたのソース内のプロパティとして

cfg.CreateMap<object, DateTime?>().ConstructUsing(src => src.ToDate()); 
+1

私はAutoMapperを使っていたのでしばらくしていましたが、同じプロパティ名を異なるタイプで自動的にマップするには、2つのタイプ(例えば、 'object'と' DateTime? ')の間のマップが必要です。ですから、 'object'と' DateTime? '(これは気になるようです)の間のマッピングを追加するか、カスタム解像度オプションを使用してください。 –

答えて

1

および宛先の種類が同じ名前の場合AutoMapperオブジェクトからDateTimeに変換しようとしますか?それは不可能で、あなたが言及したエラーを受けている理由です。

DateTimeの解決方法を定義する必要がありますか?プロパティ。これは動作します:あなたの元メンバーが有効な日付時刻オブジェクトである場合

var config = new MapperConfiguration(
    cfg => 
    { 
     cfg.CreateMap<MyClassSource, MyClassDestination>() 
      .ForMember(
       destination => destination.DateTimeValue, 
       memberOptions => memberOptions.ResolveUsing(sourceMember => 
       { 
        DateTime dateTime; 
        return !DateTime.TryParse(sourceMember.DateTimeValue.ToString(), out dateTime) ? (DateTime?) null : dateTime; 
       })); 
    } 
); 

を、それ以外の宛先プロパティがnull値を取得します、日付時刻に変換されます。

+0

nullはDateTimeの有効な値なので驚くべきことですか?変数。次のコード: 'PropertyInfo pi = typeof(MyClassDestination).GetProperty(" DateTimeValue "); \t MyClassDestination dst = new MyClassDestination(); \t pi.SetValue(dst、null); 'うまく動作します。 – costa

+1

確かに。しかし、私は、nullはDateTimeの有効な値ですが、その鍵は重要だと思いますか?ソースプロパティがオブジェクトなので、常にnullになることは保証できません。したがって、他の無効な値もある可能性があります。したがって、エラーです。 sourceプロパティの値が "Foo"(文字列)の場合はどうなりますか? –

+0

私はあなたに同意します。しかし、私はautomapperが少なくともnull値(宛先がnullableであるかぎり)または互換性のある型の値を処理するいくつかのデフォルトの変換ルーチンを提供すると考えました。 – costa

関連する問題