我试图用参数-p:PublishTrimmed=true
发布我的c#代码。它使用System.Text.JSON,并给我一个警告。/home/USER/tymaker-config-builder/Program.cs(69,33): warning IL2026: Using member 'System.Text.Json.JsonSerializer.Serialize<SerializeExtra.LetterInfo>(SerializeExtra.LetterInfo, System.Text.Json.JsonSerializerOptions?)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. [/home/USER/tymaker-config-builder/tymaker-config-builder.csproj]
。
它也给了我另一个非常相似的警告:/home/tymaker-config-builder/Program.cs(69,13): Trim analysis warning IL2026: SerializeExtra.Program.Main(): Using member 'System.Text.Json.JsonSerializer.Serialize<TValue>(TValue,JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. [/home/USER/tymaker-config-builder/tymaker-config-builder.csproj]
有没有办法解决这个问题,同时保持我的输出修剪?我不想在我的GitHub上发布~ 70 MB的可执行文件。
下面是我的代码:
using System.Text.Json;
using System.Text.Json.Serialization;
using System.IO;
namespace SerializeExtra
{
public class LetterInfo
{
public string? gift { get; set; }
public string? party { get; set; }
public string? sender { get; set; }
public string? address { get; set; }
}
public class Program
{
public static void Main()
{
Console.Write("Thank-you note bot JSON generator");
Console.Write('\n');
Console.Write("Version 1.0.0");
Console.Write("\n\n");
var letterInfo = new LetterInfo { };
Console.Write("What gift are you getting?\nThis gift will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
letterInfo.gift = Console.ReadLine();
if (letterInfo.gift == "skip")
{
letterInfo.gift = null;
}
Console.Write("\n\n");
Console.Write("What party are the thank-you notes for?\nThis party will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
letterInfo.party = Console.ReadLine();
if (letterInfo.party == "skip")
{
letterInfo.party = null;
}
Console.Write("\n\n");
Console.Write("What is your name?\nThis sender will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
letterInfo.sender = Console.ReadLine();
if (letterInfo.sender == "skip")
{
letterInfo.sender = null;
}
Console.Write("\n\n");
if (letterInfo.sender == null)
{
Console.Write("What do you want your closing word(s) to be? (e.g: {answer}, Joe)\nThis closing word will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
} else
{
Console.Write("What do you want your closing word(s) to be? (e.g: {answer}, " + letterInfo.sender + ")\nThis closing word will be used until changed.\nIf you want to skip this, type \"skip\"\n\n");
}
letterInfo.address = Console.ReadLine();
if (letterInfo.address == "skip")
{
letterInfo.address = null;
}
Console.Write("\n\n");
var options = new JsonSerializerOptions { WriteIndented = true };
string jsonString = JsonSerializer.Serialize(letterInfo, options);
string savePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
File.WriteAllText(Path.Combine(savePath, "tymaker.json"), jsonString);
Console.Write("Configuration file (tymaker.json) is saved at " + savePath + "\n\n");
}
}
}
字符串
1条答案
按热度按时间xriantvc1#
This Microsoft article解释了如何在使用System.Text.Json时使用源代码生成,这在启用修剪时是必需的。
对于您的示例,您将创建一个从
JsonSerializerContext
派生的“上下文”类。字符串
当你调用
JsonSerializer.Serialize
或JsonSerializer.Deserialize
时,你需要在上下文中传递:型
现在,如果你尝试发布,你应该看到它是成功的,没有给出这些警告:
型