Search code examples
c#xamarin.androidxamarin.iosvisual-studio-2022multitargeting

Multi-targetted Library (monoandroid, xamarinios) issue with System.ComponentModel.DataAnnotations


I'm converting a (working) .netstandard library project into a multi-targetted project that will target:

<targetframeworks>netstandard2.1;xamarinios10;monoandroid10.0;monoandroid12.0;</targetframeworks>

I'm having issues with the System.ComponentModel.DataAnnotations reference. Sepcifically I get this error (for eveything except the .netstandard target):

The type 'DataType' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.ComponentModel.DataAnnotations, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

After a bit of searching I tried adding the System.ComponentModel.Annotations (4.7.0) nuget package. This gave the same error.

In the nuget directory for this I noticed that where the .dll would normally live it had a file '.' - I didn't know if that was important (.nuget\packages\system.componentmodel.annotations\4.7.0\ref\MonoAndroid10) - the .netstandard dll was there as expected.

I've also noticed that (a separate) Xamarin.Android project seems to use the System.ComponentModel.DataAnnotations.dll which lives in C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\ReferenceAssemblies\Microsoft\Framework\Xamarin.iOS\v1.0

I'm not sure if the right version of Mono.Android (or ios equivalant) is being targetted or I have to specify a specific assembly version, or whether I'm down the wrong track completely?

Thanks in advance, Paul.


Solution

  • As the comment above mentions, to workaround this issue, I moved the bit of offending code (that used the System.ComponentModel.DataAnnotations) into a seperate project that targetted .netstandard2.1 and then referenced this project from my multi-targetted project.