Can't trim polymorphic json converter #54121
Replies: 3 comments 9 replies
-
I'm not sure how this could work. That pattern isn't trim friendly (Type.GetType based on the incoming payload? The linker has no idea what to keep). Did you turn on linker the warnings? |
Beta Was this translation helpful? Give feedback.
-
I'd replace the Type.GetType call with a dictionary lookup of known strings and types; var types = new Dictionary<string, Type>()
{
["DummyClassName"] = typeof(DummyClassName)
};
// in Read(...)
if (!types.TryGetValue(reader.GetString(), out var interfaceType))
throw new JsonException("Unknown type");
// in Write(...)
var type = types.FirstOrDefault(x => x.Value == value.GetType()).Key;
if (type == null)
throw new JsonException("Unknown type");
writer.WritePropertyName(type); I believe this approach is more linker friendly, and it also avoids the large security hole which exists when deserializing arbitrary types specified by the user. |
Beta Was this translation helpful? Give feedback.
-
What fails when you use "TrimMode link"? By default, a Blazor WASM App won't trim "user" assemblies. It only trims from assemblies that are marked with In general though, your current design (reading the type name from a payload and expecting the type to be there) can't be made trim compatible. Furthermore, |
Beta Was this translation helpful? Give feedback.
-
I have no clue how to make this polymorphic json converter trimmable:
Pubslished with:
dotnet publish --configuration Release --runtime linux-x64 --self-contained true /p:PublishTrimmed=true /p:PublishSingleFile=true
It works if I remove "TrimMode link" but I need it for a BlazorWASM App. So I decided to create this minimal console dummy that does not work.
Maybe someone can help me please🙏
Beta Was this translation helpful? Give feedback.
All reactions