You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 6 MAUI iOS应用中AutoMapper映射时崩溃问题

.NET 6 MAUI iOS下AutoMapper映射含列表属性对象崩溃问题

问题描述

.NET 6 MAUI iOS应用在使用AutoMapper映射包含列表类型属性的对象时发生崩溃,该问题仅出现在iOS平台,Android平台运行正常。当前使用最新版AutoMapper(12.0.0),旧版本也可复现该问题。复现示例项目中AutoMapper代码位于MainPage.xaml.cs中。

模型代码

public class MyClass
{
    public IEnumerable<int> Values { get; set; }
}

public class MyClassDto
{
    public IEnumerable<int> Values { get; set; }
}

AutoMapper配置与映射代码

var config = new MapperConfiguration(cfg => cfg.CreateMap<MyClass, MyClassDto>().ReverseMap());
_mapper = config.CreateMapper();

// - - - - - 

var myClass = new MyClass()
{
    Values = new List<int> { 1, 2, 3 }
};
// Android平台正常,iOS平台崩溃
var myClassDto = _mapper.Map<MyClassDto>(myClass);

崩溃堆栈跟踪

Objective-C exception thrown.  Name: AutoMapper.AutoMapperMappingException Reason: Error mapping types.

Mapping types:
MyClass -> MyClassDto
MauiAutomapper.MyClass -> MauiAutomapper.MyClassDto

Type Map configuration:
MyClass -> MyClassDto
MauiAutomapper.MyClass -> MauiAutomapper.MyClassDto

Destination Member:
Values
 (AutoMapper.AutoMapperMappingException)
   at System.Linq.Expressions.Interpreter.ThrowInstruction.Run(InterpretedFrame frame)
   at System.Linq.Expressions.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Linq.Expressions.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Linq.Expressions.Interpreter.LightLambda.Run(Object[] arguments)
   at System.Linq.Expressions.Interpreter.CallInstruction.InterpretLambdaInvoke(LightLambda targetLambda, Object[] args)
   at System.Linq.Expressions.Interpreter.FuncCallInstruction`5[[System.Func`4[[MauiAutomapper.MyClass, MauiAutomapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[MauiAutomapper.MyClassDto, MauiAutomapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[AutoMapper.ResolutionContext, AutoMapper, Version=12.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005],[MauiAutomapper.MyClassDto, MauiAutomapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[MauiAutomapper.MyClass, MauiAutomapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[MauiAutomapper.MyClassDto, MauiAutomapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[AutoMapper.ResolutionContext, AutoMapper, Version=12.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005],[MauiAutomapper.MyClassDto, MauiAutomapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Run(InterpretedFrame frame)
   at System.Linq.Expressions.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Linq.Expressions.Interpreter.LightLambda.Run(Object[] arguments)
   at MauiAutomapper.MainPage.OnCounterClicked(Object sender, EventArgs e) in /Users/sergiubulzan/Projects/tmp/MauiAutomapper/MauiAutomapper/MainPage.xaml.cs:line 23
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.Controls.Internals.IButtonElement.PropagateUpClicked()
   at Microsoft.Maui.Controls.ButtonElement.ElementClicked(VisualElement visualElement, IButtonElement ButtonElementManager)
   at Microsoft.Maui.Controls.Button.SendClicked()
   at Microsoft.Maui.Controls.Button.Microsoft.Maui.IButton.Clicked()
   at Microsoft.Maui.Handlers.ButtonHandler.OnButtonTouchUpInside(Object sender, EventArgs e)
   at UIKit.UIControlEventProxy.Activated()
Collection was modified; enumeration operation may not execute. (System.InvalidOperationException)
   at System.Collections.Generic.List`1.Enumerator[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNextRare()
   at System.Collections.Generic.List`1.Enumerator[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at System.Linq.Expressions.Interpreter.FuncCallInstruction`2[[System.Collections.IEnumerator, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Boolean, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Run(InterpretedFrame frame)
   at System.Linq.Expressions.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Linq.Expressions.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

Native stack trace:
    0   CoreFoundation                      0x0000000110104d44 __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x000000011cfb7a65 objc_exception_throw + 48
    2   libxamarin-dotnet-debug.dylib       0x000000010bb80ddf xamarin_process_managed_exception + 943
    3   MauiAutomapper                      0x0000000102b5ad29 _ZL31native_to_managed_trampoline_10P11objc_objectP13objc_selectorPP11_MonoMethodj + 361
    4   MauiAutomapper                      0x0000000102ba0959 -[UIKit_UIControlEventProxy BridgeSelector] + 41
    5   UIKitCore                           0x0000000128684c87 -[UIApplication sendAction:to:from:forEvent:] + 83
    6   UIKitCore                           0x0000000127f13988 -[UIControl sendAction:to:forEvent:] + 110
    7   UIKitCore                           0x0000000127f13d8c -[UIControl _sendActionsForEvents:withEvent:] + 345
    8   UIKitCore                           0x0000000127f1026f -[UIButton _sendActionsForEvents:withEvent:] + 148
    9   UIKitCore                           0x0000000127f125e3 -[UIControl touchesEnded:withEvent:] + 485
    10  UIKitCore                           0x0000000128147905 _UIGestureEnvironmentUpdate + 8951
    11  UIKitCore                           0x0000000128145186 -[UIGestureEnvironment _updateForEvent:window:] + 897
    12  UIKitCore                           0x00000001286c8098 -[UIWindow sendEvent:] + 5290
    13  UIKitCore                           0x000000012869e160 -[UIApplication sendEvent:] + 820
    14  UIKitCore                           0x0000000128736cfd __dispatchPreprocessedEventFromEventQueue + 8713
    15  UIKitCore                           0x00000001287394a0 __processEventQueue + 8635
    16  UIKitCore                           0x000000012872fccd __eventFetcherSourceCallback + 232
    17  CoreFoundation                      0x0000000110071833 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    18  CoreFoundation                      0x000000011007172b __CFRunLoopDoSource0 + 180
    19  CoreFoundation                      0x0000000110070bf8 __CFRunLoopDoSources0 + 242
    20  CoreFoundation                      0x000000011006b2f4 __CFRunLoopRun + 871
    21  CoreFoundation                      0x000000011006aa90 CFRunLoopRunSpecific + 562
    22  GraphicsServices                    0x000000011fd05c8e GSEventRunModal + 139
    23  UIKitCore                           0x000000012867e90e -[UIApplication _run] + 928
    24  UIKitCore                           0x0000000128683569 UIApplicationMain + 101
    25  libmonosgen-2.0.dylib               0x000000010c36ec42 do_icall + 194
    26  libmonosgen-2.0.dylib               0x000000010c36dc5d do_icall_wrapper + 253
    27  libmonosgen-2.0.dylib               0x000000010c35fe7a interp_exec_method + 2970
    28  libmonosgen-2.0.dylib               0x000000010c35e0ff interp_runtime_invoke + 239
    29  libmonosgen-2.0.dylib               0x000000010c255d3b mono_jit_runtime_invoke + 1227
    30  libmonosgen-2.0.dylib               0x000000010c178328 mono_runtime_invoke_checked + 136
    31  libmonosgen-2.0.dylib               0x000000010c17f0cb mono_runtime_exec_main_checked + 107
    32  libmonosgen-2.0.dylib               0x000000010c2b81e2 mono_jit_exec + 354
    33  libxamarin-dotnet-debug.dylib       0x000000010bb94e9d xamarin_main + 1949
    34  MauiAutomapper                      0x0000000102bec954 main + 52
    35  dyld                                0x000000010b768f21 start_sim + 10
    36  ???                                 0x000000020315651e 0x0 + 8641668382
    37  ???                                 0x0000000000000003 0x0 + 3

内容的提问来源于stack exchange,提问作者buseco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 08:45:46