Utf8StringInterpolation
Utf8StringInterpolation copied to clipboard
Unable to make it work in a setup where a library using this package is shared between .NET 9 and .NET 472 projects
Attached are the following files:
C:\work\test [master]> tree /f
Folder PATH listing for volume Windows
Volume serial number is 268C-52B1
C:.
│ .gitignore
│ Test.sln
│
├───Exe
│ Exe.csproj
│ Program.cs
│
└───Lib
Extensions.cs
Lib.csproj
ToStringJsonConverter.cs
C:\work\test [master]>
The problem - the same code works in net472 and does not work in net9.0:
net472
C:\work\test [master]> Exe\bin\Debug\net472\Exe.exe
Hello World!
"Hello World!"
"Hello World!"
C:\work\test [master]>
net9.0
C:\work\test [master]> Exe\bin\Debug\net9.0\Exe.exe
Hello World!
"Hello World!"
Unhandled exception. System.TypeLoadException: Could not load type 'System.Buffers.ArrayBufferWriter`1' from assembly 'Utf8StringInterpolation, Version=1.3.2.0, Culture=neutral, PublicKeyToken=df4c250b14d82627'.
at Lib.Extensions.GetString()
at Exe.Program.LibraryTestObject.ToString() in C:\work\test\Exe\Program.cs:line 18
at DFModuleEnum.UnitTests.ToStringJsonConverter.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer) in C:\work\test\Lib\ToStringJsonConverter.cs:line 10
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter writer, JsonConverter converter, Object value, JsonContract contract, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.SerializeObject(Object value)
at Exe.Program.Main() in C:\work\test\Exe\Program.cs:line 25
C:\work\test [master]>
Here is the full code:
.\Exe\Exe.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net9.0;net472</TargetFrameworks>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Lib\Extensions.cs" Link="Extensions.cs" />
<ProjectReference Include="..\Lib\Lib.csproj" />
</ItemGroup>
</Project>
.\Exe\Program.cs
using DFModuleEnum.UnitTests;
using Newtonsoft.Json;
using System;
namespace Exe;
public static class Program
{
[JsonConverter(typeof(ToStringJsonConverter))]
public class LocalTestObject
{
override public string ToString() => Extensions.GetString();
}
[JsonConverter(typeof(ToStringJsonConverter))]
public class LibraryTestObject
{
override public string ToString() => Lib.Extensions.GetString();
}
public static void Main()
{
Console.WriteLine(Extensions.GetString());
Console.WriteLine(JsonConvert.SerializeObject(new LocalTestObject()));
Console.WriteLine(JsonConvert.SerializeObject(new LibraryTestObject()));
}
}
.\Lib\Lib.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<DefineConstants>LIBRARY</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Utf8StringInterpolation" Version="1.3.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>
.\Lib\Extensions.cs
using Utf8StringInterpolation;
#if LIBRARY
namespace Lib;
#else
namespace Exe;
#endif
public static class Extensions
{
public static string GetString()
{
using (var buffer = Utf8String.CreateWriter(out var zsb))
{
zsb.Append("Hello World!");
zsb.Flush();
return buffer.ToString();
}
}
}
.\Lib\ToStringJsonConverter.cs
using Newtonsoft.Json;
using System;
namespace DFModuleEnum.UnitTests;
public class ToStringJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => true;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => writer.WriteValue(value.ToString());
public override bool CanRead => false;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) => throw new NotImplementedException();
}