Repository navigation
Description
If an application uses the Microsoft.XmlSerializer.Generator to generate an assembly for xml serialization/deserialization and the application gets trimmed by the ILLinker, the linker won't be able to recognize that it should keep the code in the generated assembly.
This is because we are dynamically loading the assembly:
runtime/src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compilation.cs
Lines 172 to 175 in 1d70b31
| if (!string.IsNullOrEmpty(serializerPath)) | |
| { | |
| serializer = Assembly.LoadFile(serializerPath); | |
| } |
And then using reflection to grab a type out of it:
runtime/src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compilation.cs
Line 236 in 1d70b31
| Type contractType = GetTypeFromAssembly(serializer, "XmlSerializerContract"); |
runtime/src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compilation.cs
Lines 514 to 521 in 1d70b31
| internal static Type GetTypeFromAssembly(Assembly assembly, string typeName) | |
| { | |
| typeName = GeneratedAssemblyNamespace + "." + typeName; | |
| Type type = assembly.GetType(typeName); | |
| if (type == null) | |
| throw new InvalidOperationException(SR.Format(SR.XmlMissingType, typeName, assembly.FullName)); | |
| return type; | |
| } |
The linker won't be able to know to analyze the generated assembly because it is not referenced by the application. And if/when it did analyze the generated assembly, it wouldn't know to keep the generated XmlSerializerContract type.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
MichalStrehovsky commentedon Jun 12, 2020
Other serializers that offer pregeneration solve this by asking users to explicitly initialize the generated code - this adds a static reference to it that linker can analyze: e.g. this is how Utf8json does this: https://github.com/neuecc/Utf8Json/#pre-code-generationunityxamarin-supports
We should ideally add something like that (if the generated code is otherwise supportable with linker - not sure about that).
dotnet-policy-service commentedon Jan 7, 2025
Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.
This process is part of our issue cleanup automation.
dotnet-policy-service commentedon Jan 21, 2025
This issue will now be closed since it had been marked
no-recent-activitybut received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.