Xamarin iOS Linker causing AutoMapper issues

671 Views Asked by At

I am working on a Xamarin project that is using AutoMapper. When linking is set to "Link Framework SDKs Only" I get the following error when initializing the maps

System.ArgumentNullException: Value cannot be null. Parameter name: method

The exception is not much to go on and neither is the callstack

AutoMapper.Mappers.ConvertMapper.MapExpression
AutoMapper.Execution.TypeMapPlanBuilder.ObjectMapperExpression
AutoMapper.Execution.TypeMapPlanBuilder.MapExpression
AutoMapper.Mappers.NullableSourceMapper.MapExpression
AutoMapper.Execution.TypeMapPlanBuilder.ObjectMapperExpression
AutoMapper.Execution.TypeMapPlanBuilder.MapExpression
AutoMapper.Execution.TypeMapPlanBuilder.MapExpression
AutoMapper.Execution.TypeMapPlanBuilder.CreatePropertyMapFunc
AutoMapper.Execution.TypeMapPlanBuilder.CreatePropertyMapFunc
AutoMapper.Execution.TypeMapPlanBuilder.TryPropertyMap
AutoMapper.Execution.TypeMapPlanBuilder.CreateAssignmentFunc
AutoMapper.Execution.TypeMapPlanBuilder.CreateMapperLambda
AutoMapper.TypeMap.Seal AutoMapper.MapperConfiguration.Seal
AutoMapper.MapperConfiguration..ctor
AutoMapper.MapperConfiguration..ctor AutoMapper.Mapper.Initialize

The initial reading of that suggests that the Linker is simply removing some properties or indeed methods from some class we are using.

However, after commenting out most of the mappings and then reintroducing them one at a time I find that this class is currently causing the error.

[Preserve]
internal class ItemBase : CareRecordItemBase
{
      [Preserve]
      public string Topic { get; set; }
    
      [Preserve]
      public string InPractice { get; set; }
    
      [Preserve]
      public string PrivateVal { get; set; }
}

If I comment out the InPractice property the exception does not get thrown. This makes no sense to me. Can anyone explain why this might be a problem?

Are there better ways of diagnosing these issues?

2

There are 2 best solutions below

0
Pat Long - Munkii Yebee On BEST ANSWER

See comment from AutoMapper author Lucian Bargaoanu. I needed to include an xml file that adds configuration to the Linking process.

Lucian refers to this Automapper Issue, https://github.com/AutoMapper/AutoMapper/issues/2272

Also see

Custom Linker configuration

1
Iain Smith On

Based on Pat's Answer:

  1. Add an XML file LinkDescription.xml, you can name it anything you want.
  2. Add this code:
<linker> 
    <assembly fullname="mscorlib">
        <type fullname="System.Convert" preserve="All" />
    </assembly>
</linker>
  1. Set the file's build action to LinkDescription