@@ -54,6 +54,97 @@ public UANodeSet()
5454 {
5555 }
5656
57+ /// <summary>
58+ /// Gets a cached <see cref="XmlSerializer"/> instance for
59+ /// <see cref="UANodeSet"/>. The serializer is lazily created and
60+ /// reused for all subsequent calls.
61+ /// </summary>
62+ /// <remarks>
63+ /// The suppression is safe because <see cref="UANodeSet"/> and all
64+ /// reachable types in the object graph are annotated with
65+ /// <see cref="XmlRootAttribute"/>, <see cref="XmlTypeAttribute"/>,
66+ /// <see cref="XmlIncludeAttribute"/>, and element/attribute
67+ /// mapping attributes. The NativeAOT linker preserves these types
68+ /// through the static attribute references.
69+ /// </remarks>
70+ internal static XmlSerializer Serializer => s_serializer . Value ;
71+
72+ [ UnconditionalSuppressMessage ( "AOT" ,
73+ "IL3050:RequiresDynamicCode" ,
74+ Justification = "UANodeSet and all reachable types are fully " +
75+ "annotated with XML serialization attributes." ) ]
76+ [ UnconditionalSuppressMessage ( "Trimming" ,
77+ "IL2026:RequiresUnreferencedCode" ,
78+ Justification = "UANodeSet and all reachable types are fully " +
79+ "annotated with XML serialization attributes." ) ]
80+ #if NET5_0_OR_GREATER
81+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UANodeSet ) ) ]
82+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( ModelTableEntry ) ) ]
83+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( NodeIdAlias ) ) ]
84+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UANode ) ) ]
85+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UAType ) ) ]
86+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UAInstance ) ) ]
87+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UAObject ) ) ]
88+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UAVariable ) ) ]
89+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UAMethod ) ) ]
90+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UAView ) ) ]
91+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UAObjectType ) ) ]
92+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UAVariableType ) ) ]
93+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UAReferenceType ) ) ]
94+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UADataType ) ) ]
95+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( DataTypeDefinition ) ) ]
96+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( DataTypeField ) ) ]
97+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( Reference ) ) ]
98+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( LocalizedText ) ) ]
99+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( RolePermission ) ) ]
100+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UAMethodArgument ) ) ]
101+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( TranslationType ) ) ]
102+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( StructureTranslationType ) ) ]
103+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( NodeSetStatus ) ) ]
104+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( NodeToDelete ) ) ]
105+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( ReferenceChange ) ) ]
106+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UANodeSetChanges ) ) ]
107+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( UANodeSetChangesStatus ) ) ]
108+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( ReleaseStatus ) ) ]
109+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( DataTypePurpose ) ) ]
110+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( System . Xml . XmlElement ) ) ]
111+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( System . Xml . XmlDocument ) ) ]
112+ [ DynamicDependency ( DynamicallyAccessedMemberTypes . All , typeof ( System . Xml . XmlNode ) ) ]
113+ #endif
114+ private static XmlSerializer CreateSerializer ( )
115+ {
116+ return new XmlSerializer ( typeof ( UANodeSet ) ) ;
117+ }
118+
119+ private static readonly Lazy < XmlSerializer > s_serializer =
120+ new Lazy < XmlSerializer > ( CreateSerializer ) ;
121+
122+ #if NET5_0_OR_GREATER
123+ /// <summary>
124+ /// Serializes a <see cref="UANodeSet"/> to an <see cref="XmlWriter"/>
125+ /// using the pre-generated serializer code. This avoids the
126+ /// reflection-based fallback which fails under NativeAOT.
127+ /// </summary>
128+ private static void SerializePreGen ( XmlWriter writer , UANodeSet nodeSet )
129+ {
130+ var serWriter = new UANodeSetXmlSerializerWriter ( writer ) ;
131+ serWriter . Write26_UANodeSet ( nodeSet ) ;
132+ }
133+
134+ /// <summary>
135+ /// Exposes the protected <see cref="XmlSerializationWriter.Writer"/>
136+ /// property for direct use outside <see cref="XmlSerializer"/>.
137+ /// </summary>
138+ private sealed class UANodeSetXmlSerializerWriter
139+ : Microsoft . Xml . Serialization . GeneratedAssembly . XmlSerializationWriterUANodeSet
140+ {
141+ public UANodeSetXmlSerializerWriter ( XmlWriter w )
142+ {
143+ Writer = w ;
144+ }
145+ }
146+ #endif
147+
57148 /// <summary>
58149 /// Validate the nodeset against the schema.
59150 /// </summary>
@@ -120,31 +211,45 @@ void ValidationEventHandler(object sender, ValidationEventArgs e)
120211 /// </summary>
121212 /// <param name="istrm">The input stream.</param>
122213 /// <returns>The set of nodes</returns>
123- [ RequiresUnreferencedCode ( "Uses XmlSerializer which requires unreferenced code." ) ]
124- [ RequiresDynamicCode ( "Uses XmlSerializer which requires unreferenced code." ) ]
214+ [ UnconditionalSuppressMessage ( "AOT" ,
215+ "IL3050:RequiresDynamicCode" ,
216+ Justification = "UANodeSet and all reachable types are fully " +
217+ "annotated with XML serialization attributes." ) ]
218+ [ UnconditionalSuppressMessage ( "Trimming" ,
219+ "IL2026:RequiresUnreferencedCode" ,
220+ Justification = "UANodeSet and all reachable types are fully " +
221+ "annotated with XML serialization attributes." ) ]
125222 public static UANodeSet Read ( Stream istrm )
126223 {
127224 using var reader = new StreamReader ( istrm ) ;
128225 using var xmlReader = XmlReader . Create ( reader , CoreUtils . DefaultXmlReaderSettings ( ) ) ;
129- var serializer = new XmlSerializer ( typeof ( UANodeSet ) ) ;
130- return serializer . Deserialize ( xmlReader ) as UANodeSet ;
226+ return Serializer . Deserialize ( xmlReader ) as UANodeSet ;
131227 }
132228
133229 /// <summary>
134230 /// Write a nodeset to a stream.
135231 /// </summary>
136232 /// <param name="istrm">The input stream.</param>
137- [ RequiresUnreferencedCode ( "Uses XmlSerializer which requires unreferenced code." ) ]
138- [ RequiresDynamicCode ( "Uses XmlSerializer which requires unreferenced code." ) ]
233+ [ UnconditionalSuppressMessage ( "AOT" ,
234+ "IL3050:RequiresDynamicCode" ,
235+ Justification = "UANodeSet and all reachable types are fully " +
236+ "annotated with XML serialization attributes." ) ]
237+ [ UnconditionalSuppressMessage ( "Trimming" ,
238+ "IL2026:RequiresUnreferencedCode" ,
239+ Justification = "UANodeSet and all reachable types are fully " +
240+ "annotated with XML serialization attributes." ) ]
139241 public void Write ( Stream istrm )
140242 {
141243 XmlWriterSettings setting = CoreUtils . DefaultXmlWriterSettings ( ) ;
142244 var writer = XmlWriter . Create ( istrm , setting ) ;
143245
144246 try
145247 {
146- var serializer = new XmlSerializer ( typeof ( UANodeSet ) ) ;
147- serializer . Serialize ( writer , this , null ) ;
248+ #if NET5_0_OR_GREATER
249+ SerializePreGen ( writer , this ) ;
250+ #else
251+ Serializer . Serialize ( writer , this , null ) ;
252+ #endif
148253 }
149254 finally
150255 {
0 commit comments