The type 'Memory<T>' exists in both 'System.Memory 4.0.1.0 and mscorlib 2.0.5.0

Viewed 444

i'm having hard time resolving this new issue.
It appears only on the xamarin.ios project of the solution at compile time.
The xamarin.android project is compiling ok.

I did not reference system.memory but netstandard 2.0 projects.
That was compiling fine until recently.

Is it a known issue in xamarin.ios tooling ? any workaround ?

I've tried:

  • googling
  • add the system.memory nuget to the ios project
  • delete bin/obj/.vs folders, clean/rebuild

I'm dry.

2 Answers

I found the solution here !

Add at the end of your ios project file:

  <Target 
    Name="VS16_RemoveSystemMemory" 
    BeforeTargets="FindReferenceAssembliesForReferences" 
    Condition="'$(MSBuildVersion)' &gt;= '16.0'">
    <ItemGroup>
      <_ReferencePathToRemove 
        Include="@(ReferencePath)" 
        Condition="'%(ReferencePath.NuGetPackageId)'=='System.Memory'" />
      <ReferencePath Remove="@(_ReferencePathToRemove)" />
    </ItemGroup>
    <Message Text="Removing System.Memory for VS 2019 compatibility" Importance="high"/>
  </Target>

And magically it compiles !
I don't know from where this nuget dependency came, but it's not from my code, it's from a third party nuget, most probably from the Google ones.

For anyone that arrives here on a related issue around Span<T> or ReadOnlySpan<T> being ambiguous in Xamarin iOS with .NET Standard 2.1 dependencies, the issue is still related to System.Memory but the answer pointed out by Softlion did not solve for me. The reasoning and workaround pointed out in this [thread][2] was my underlying problem:

There are two types of the same name System.ReadOnlySpan in the app declared and cannot be unified.

The first one is declared in System.Memory.dll (one implementation)

The second one is in netstandard.dll (the type-forwarder to the second implementation inside mscorlib.dll)

The bug is in System.Memory nuget because it's missing netstandard2.1 TFM implementation where the System.ReadOnlySpan type would not be declared in the assembly but referenced from netstandard.dll

Adding package references for System.Memory 4.5.4 and System.Buffers 4.5.1 was the workaround I needed.

Related