How can I tell the Compiler which Members of a Type cannot be trimmed in a given context?

72 Views Asked by At

Given the following utility method that parses a JSON file:

public static T? ReadJsonFile
<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>
(String file)
{
    using var fileStream = System.IO.File.OpenRead($"./Resources/{file}");
    return System.Text.Json.JsonSerializer.Deserialize<T>(fileStream);
}

I want to tell the compiler/trimmer, that none of the public properties of T can be trimmed, even if they are not used in the code.

Does my usage of the DynamicallyAccessedMembersAttribute accomplish this goal?

Compiling still produces a trim warning for Deserialize.
Why is that, and how can I 'safely' get rid of it?

Is there a better/cleaner way of achieving what the method does using other overloads of Deserialize?

0

There are 0 best solutions below