The problem occurs between a Xamarin.IOS type project and a .NetStandard2.0 library. I have created a small solution (link here) demonstrating the problem. To achieve this, the Xamarin project uses the Newtonsoft nuget.
Situation: In the .NetStandard project, I have a single object containing a single property (in my case, a property of type HttpStatusCode) with a custom attribute defining the default value of this property.
public class TestObject
{
[DefaultValue(HttpStatusCode.OK)]
public HttpStatusCode Code { get; set; }
}
In the Xamarin project, I created a single test which instantiated an object of the type created in the .NetStandard project and which tried to serialize it.
[Test]
public void Test()
{
TestObject obj = new TestObject(code: HttpStatusCode.Accepted);
JsonConvert.SerializeObject(obj);
}
When running the test, the error returned is as follows: System.TypeLoadException: Could not load type System.Net.HttpStatusCode, netstandard, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = cc7b13ffcd2ddd51 while decoding custom attribute: (null )
In my situation, it is required that the architecture defined in the csproj is of type * ARMv7 + ARM64 * for the sake of compatibility with the old Ipads that we have to support. I mention this because I know that choosing Linker behavior to Link All and Supported architechtures to ARM64 makes the test pass without problems.
Edit
Answer to the question Why the need to use 2 projects: In fact, the solution provided in this post is just a tiny excerpt from a much larger project. In this project, we provide a Windows version (.NetCore 3.1) and an IOS version (Xamarin.IOS) of the application. To share our models between these 2 different projects, we would like to use a .NetStandard type library, instead of separating this library into Windows and IOS projects. We are open to other architectural solution proposals if that can solve our problem, but for now that's all we have.
If you have any further questions about the problem and its context, don't hesitate to ask.
Thanks in advance for your help