AutoMapper Mapper.Initialize 导致 System.Linq.Expressions 中的 ArgumentNullException (Xamarin.iOS)

B B*_*and 4 automapper xamarin.ios

我正在尝试将 AutoMapper 集成到我的 Xamarin Forms 应用程序中。它在 Android 设备和模拟器中都能成功运行,但在 iOS 中我只能让它在模拟器中运行。在真实设备上运行它会导致 System.ArgumentNullException。

这是我的 App.xaml.cs OnStart 方法中的 Mapper.Initialize 代码:

// AutoMapper config
Mapper.Initialize(cfg =>
{
    cfg.AddProfile<MappingProfile>();
});
Run Code Online (Sandbox Code Playgroud)

这是堆栈跟踪:

System.ArgumentNullException: Value cannot be null.
Parameter name: method
  at System.Linq.Expressions.Expression.Call (System.Reflection.MethodInfo method, System.Linq.Expressions.Expression arg0) [0x0003e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.0.0.0/src/mono/external/corefx/src/System.Linq.Expressions/src/System/Linq/Expressions/MethodCallExpression.cs:879
  at AutoMapper.Mappers.ConvertMapper.ConvertExpression (System.Type sourceType, System.Type destinationType) [0x00040] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at AutoMapper.Mappers.ConvertMapper+<>c__DisplayClass1_1.<GetConverters>b__4 () [0x00000] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at System.Lazy`1[T].CreateValue () [0x00081] in <6314851f133e4e74a2e96356deaa0c6c>:0
  at System.Lazy`1[T].LazyInitValue () [0x0000c] in <6314851f133e4e74a2e96356deaa0c6c>:0
  at System.Lazy`1[T].get_Value () [0x0003a] in <6314851f133e4e74a2e96356deaa0c6c>:0
  at AutoMapper.Mappers.ConvertMapper.MapExpression (AutoMapper.IConfigurationProvider configurationProvider, AutoMapper.ProfileMap profileMap, AutoMapper.PropertyMap propertyMap, System.Linq.Expressions.Expression sourceExpression, System.Linq.Expressions.Expression destExpression, System.Linq.Expressions.Expression contextExpression) [0x00021] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at AutoMapper.Execution.TypeMapPlanBuilder.ObjectMapperExpression (AutoMapper.IConfigurationProvider configurationProvider, AutoMapper.ProfileMap profileMap, AutoMapper.TypePair typePair, System.Linq.Expressions.Expression sourceParameter, System.Linq.Expressions.Expression contextParameter, AutoMapper.PropertyMap propertyMap, System.Linq.Expressions.Expression destinationParameter) [0x0000b] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at AutoMapper.Execution.TypeMapPlanBuilder.MapExpression (AutoMapper.IConfigurationProvider configurationProvider, AutoMapper.ProfileMap profileMap, AutoMapper.TypePair typePair, System.Linq.Expressions.Expression sourceParameter, System.Linq.Expressions.Expression contextParameter, AutoMapper.PropertyMap propertyMap, System.Linq.Expressions.Expression destinationParameter) [0x0006d] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at AutoMapper.Execution.TypeMapPlanBuilder.MapExpression (AutoMapper.TypePair typePair, System.Linq.Expressions.Expression sourceParameter, AutoMapper.PropertyMap propertyMap, System.Linq.Expressions.Expression destinationParameter) [0x00011] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at AutoMapper.Execution.TypeMapPlanBuilder.CreatePropertyMapFunc (AutoMapper.PropertyMap propertyMap, System.Linq.Expressions.Expression destination) [0x000de] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at AutoMapper.Execution.TypeMapPlanBuilder.CreatePropertyMapFunc (AutoMapper.PropertyMap propertyMap) [0x00000] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at AutoMapper.Execution.TypeMapPlanBuilder.TryPropertyMap (AutoMapper.PropertyMap propertyMap) [0x00000] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at AutoMapper.Execution.TypeMapPlanBuilder.CreateAssignmentFunc (System.Linq.Expressions.Expression destinationFunc, System.Boolean constructorMapping) [0x00044] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at AutoMapper.Execution.TypeMapPlanBuilder.CreateMapperLambda (System.Collections.Generic.HashSet`1[T] visitedTypeMaps) [0x000bb] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at AutoMapper.TypeMap.Seal (AutoMapper.IConfigurationProvider configurationProvider, System.Collections.Generic.HashSet`1[T] visitedTypeMaps) [0x00088] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at AutoMapper.MapperConfiguration.Seal () [0x001f8] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at AutoMapper.MapperConfiguration..ctor (AutoMapper.Configuration.MapperConfigurationExpression configurationExpression) [0x00177] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at AutoMapper.MapperConfiguration..ctor (System.Action`1[T] configure) [0x00007] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at AutoMapper.Mapper.Initialize (System.Action`1[T] config) [0x00000] in <0cc4ddabcc77416ea712cf2d35abef51>:0
  at Mobile.App.OnStart () [0x0002c]
Run Code Online (Sandbox Code Playgroud)

我已确保 Xcode、NuGet 包和 Visual Studio 都是最新的。

任何帮助表示赞赏!

jfm*_*fmg 5

答案是,您需要在项目的根目录中包含LinkDescription.xml ,并将构建设置设置为LinkDescription和以下内容:

<linker> 
  <assembly fullname="mscorlib">
    <type fullname="System.Convert" preserve="All" />
  </assembly>
</linker>

Run Code Online (Sandbox Code Playgroud)

旁注:我尝试将此行添加到附加 mtouch 参数中,如https://learn.microsoft.com/en-us/xamarin/ios/deploy-test/linker--linkskip System.Convert中所述,但它不起作用。

只有 LinkDescription.xml 有效。有关 LinkDescription.xml 如何工作的参考可以在此处找到: https: //learn.microsoft.com/en-us/learn/modules/prepare-to-publish-your-xamarin-application/5-preserve-ritic-代码

GitHub 上还有一个关于此问题的已关闭问题,它提出了相同的解决方案:https ://github.com/AutoMapper/AutoMapper/issues/2272