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

AutoMapper Mapper.Initialize导致System.Linq.Expressions(Xamarin.iOS)中的ArgumentNullException,xamarin.ios,automapper,Xamarin.ios,Automapper,我正在尝试将AutoMapper集成到我的Xamarin表单应用程序中。它成功地在Android设备和模拟器中工作,但在iOS中,我只能让它在模拟器中工作。在真实设备上运行会导致System.ArgumentNullException 这是我的映射程序。初始化App.xaml.cs OnStart方法中的代码: // AutoMapper config Mapper.Initialize(cfg => { cfg.AddProfile<MappingProfile>()

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

这是我的映射程序。初始化App.xaml.cs OnStart方法中的代码:

// AutoMapper config
Mapper.Initialize(cfg =>
{
    cfg.AddProfile<MappingProfile>();
});
//自动映射配置
Mapper.Initialize(cfg=>
{
AddProfile();
});
以下是堆栈跟踪:

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]
System.ArgumentNullException:值不能为null。
参数名称:方法
在/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中的System.Linq.Expressions.Expressions.Call(System.Reflection.MethodInfo方法,System.Linq.Expressions.Expressions.arg0)[0x0003e]
在AutoMapper.Mappers.ConvertMapper.ConvertExpression(System.Type sourceType,System.Type destinationType)[0x00040]中:0
在AutoMapper.Mappers.ConvertMapper+c_uuuu中显示Class1_1.b_uuu4()[0x00000]in:0
在0中的System.Lazy`1[T].CreateValue()[0x00081]处
在System.Lazy`1[T].LazyInitValue()[0x0000c]中:0
在System.Lazy`1[T]处,在以下位置获取_值()[0x0003a]:0
在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.contextExpression)[0x00021]年:0
位于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]输入:0
位于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]输入:0
在AutoMapper.Execution.TypeMapPlanBuilder.MapExpression(AutoMapper.TypePair TypePair,System.Linq.Expressions.Expression sourceParameter,AutoMapper.PropertyMap PropertyMap,System.Linq.Expressions.Expression destinationParameter)[0x00011]中:0
在AutoMapper.Execution.TypeMapPlanBuilder.CreatePropertyMapFunc(AutoMapper.PropertyMap PropertyMap,System.Linq.Expressions.Expression destination)[0x000de]中:0
在0中的AutoMapper.Execution.TypeMapPlanBuilder.CreatePropertyMapFunc(AutoMapper.PropertyMap PropertyMap PropertyMap)[0x00000]处
位于AutoMapper.Execution.TypeMapPlanBuilder.TryPropertyMap(AutoMapper.PropertyMap PropertyMap)[0x00000]中:0
在AutoMapper.Execution.TypeMapPlanBuilder.CreateSignmentFunc(System.Linq.Expressions.Expression destinationFunc,System.Boolean constructorMapping)[0x00044]中:0
在AutoMapper.Execution.TypeMapPlanBuilder.CreateMapperLambda(System.Collections.Generic.HashSet`1[T]visitedTypeMaps)[0x000bb]中:0
在AutoMapper.TypeMap.Seal(AutoMapper.IConfigurationProvider configurationProvider,System.Collections.Generic.HashSet`1[T]visitedTypeMaps)[0x00088]中:0
在AutoMapper.MapperConfiguration.Seal()[0x001f8]中:0
在AutoMapper.MapperConfiguration..ctor(AutoMapper.Configuration.MapperConfigurationExpression configurationExpression)[0x00177]中:0
在AutoMapper.MapperConfiguration..ctor(系统操作'1[T]配置)[0x00007]中:0
在0中的AutoMapper.Mapper.Initialize(System.Action`1[T]config)[0x00000]处
在Mobile.App.OnStart()[0x0002c]
我已经确保Xcode、NuGet包和VisualStudio都是最新的


感谢您的帮助!

右键单击iOS项目>属性>将链接器行为设置为“不链接”


属性>将链接器行为设置为“不链接”>

答案是您需要在项目的根目录中包含一个LinkDescription.xml,生成设置设置为LinkDescription,并包含以下内容:

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


但它没有起作用

只有LinkDescription.xml工作。有关LinkDescription.xml工作方式的参考信息,请参见:


GitHub上还有一个关于此问题的已解决问题,建议使用相同的解决方案:

尝试降级AutoMapper,但未解决此问题。还尝试使用直接的对象到对象映射,但运气不佳:(认为这是同一个问题。已设置为“不链接”,问题已得到解决。但不理想。这将大幅增加ipa大小,因为添加到项目中的所有库都不会被链接和剥离!它可以工作,但不建议用于发布配置。感谢此解决方案。对于其他人,我发现此解决方案有效,但我也尝试添加“-linkskip=mscorlib”,这也很有效。