试图在AutoMapper中使用自定义解析器,抛出Object Ref Error

时间:2016-12-22 12:16:25

标签: c# automapper

我收到了错误

  

{"对象引用未设置为对象的实例。"}

尝试使用自定义解析器时(强制任何Null返回使用一组默认值),如下所示:

public class AnswersResolver : IValueResolver<SurveyMonkey.Containers.Question, Question, QuestionAnswers>
{
    public QuestionAnswers Resolve(SurveyMonkey.Containers.Question source, Question destination, QuestionAnswers destMember, ResolutionContext context)
    {
        if (source.Answers != null)
        {
            QuestionAnswers q = Mapper.Map<QuestionAnswers>(source.Answers);
            return q;
        }
        else
        {
            QuestionAnswers q = new QuestionAnswers
            {
                Id = source.Id * 100000,
                Choices = {},
                Cols = { new Column {  Choices = {  } } },
                Other = new OtherAnswer { Text = "NO ANSWER" },
                Rows = { new Row {  Text = "NO ANSWER" } }
            };
            Choice c = new Choice { Description = "No ANSWER REQUIRED", Position = 0, Items = { }, QuestionAnswersId = q.Id.GetValueOrDefault(), QuestionAnswers = q };
            q.Choices.Add(c);
            q.Cols[0].Choices.Add(c);
            q.Cols[0].QuestionAnswers = q;
            q.Cols[0].QuestionAnswersId = q.Id.GetValueOrDefault();
            q.Rows[0].QuestionAnswers = q;
            q.Rows[0].QuestionAnswersId = q.Id.GetValueOrDefault();
            return q;
        }
    }
}

当我输入一个断点时,我成功地循环了几次迭代,并且在用于Survey的Automapper.map命令中抛出错误 - 其中包含Question并包含包含(某些)相同问题对象的Page。

编辑:请求的错误消息

  

AutoMapper.AutoMapperMappingException未处理      的HResult = -2146233088      消息=错误映射类型。

     

映射类型:      调查 - &gt;调查      SurveyMonkey.Containers.Survey - &gt; SurveyMonkeyAPIv3.Survey

     

类型地图配置:      调查 - &gt;调查      SurveyMonkey.Containers.Survey - &gt; SurveyMonkeyAPIv3.Survey

     

属性:      问题        Source =匿名托管DynamicMethods程序集        堆栈跟踪:           在lambda_method(Closure,Object,Object,ResolutionContext)           在SurveyMonkeyAPIv3.Program.DefinedSurveyCalls(SurveyContext Survey,String APIKey,String Token,Int64 [] ValidSurveys)中的C:\ Surveymonkeytake2 \ SurveyMonkeyAPIv3 \ Program.cs:第77行           在SurveyMonkeyAPIv3.Program.Main(String [] args)中的C:\ Surveymonkeytake2 \ SurveyMonkeyAPIv3 \ Program.cs:第41行           在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)           在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)           在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()           在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)           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)           在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)           在System.Threading.ThreadHelper.ThreadStart()      的InnerException:           的HResult = -2146233088           消息=错误映射类型。

     

映射类型:      问题 - &gt;题      SurveyMonkey.Containers.Question - &gt; SurveyMonkeyAPIv3.Question

     

类型地图配置:      问题 - &gt;题      SurveyMonkey.Containers.Question - &gt; SurveyMonkeyAPIv3.Question

     

属性:      答案         Source =匿名托管DynamicMethods程序集         堆栈跟踪:              在lambda_method(Closure,Object,Object,ResolutionContext)         的InnerException:              的HResult = -2147467261              Message =对象引用未设置为对象的实例。              来源= SurveyMonkeyAPIv3              堆栈跟踪:                   在SurveyMonkeyAPIv3.AnswersResolver.Resolve(问题来源,问题目的地,QuestionAnswers destMember,ResolutionContext上下文)中的C:\ Surveymonkeytake2 \ SurveyMonkeyAPIv3 \ Program.cs:第854行                   在lambda_method(Closure,Object,Object,ResolutionContext)              InnerException:

在最后一次内部异常之后没有文字

1 个答案:

答案 0 :(得分:0)

我一直无法解决这个问题,但是我通过将引用设置为nullable来解决这个问题,添加

.ForMember(T => T.Answers, s => s.Condition(so=> so!=null ))

到CreateMap for Question和(在一些摆弄主键之后),返回一组转换后的数据。