1

コード:

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

1 に答える 1

4

ソース タイプと宛先タイプのプロパティが同じ名前であるため、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 値を取得します。

于 2016-10-17T20:37:49.040 に答える