@@ -157,6 +157,21 @@ void Analyze(NameService service, ConfuserContext context, ProtectionParameters
157157 service . SetCanRename ( type , false ) ;
158158 }
159159
160+ /*
161+ * Can't rename Classes/Types that will be serialized
162+ */
163+ if ( type != null ) {
164+ if ( type . IsSerializable ) {
165+ service . SetCanRename ( type , false ) ;
166+ }
167+
168+ if ( type . DeclaringType != null ) {
169+ if ( type . DeclaringType . IsSerializable ) {
170+ service . SetCanRename ( type , false ) ;
171+ }
172+ }
173+ }
174+
160175 if ( parameters . GetParameter ( context , type , "forceRen" , false ) )
161176 return ;
162177
@@ -200,9 +215,30 @@ void Analyze(NameService service, ConfuserContext context, ProtectionParameters
200215 else if ( parameters . GetParameter ( context , field , "forceRen" , false ) )
201216 return ;
202217
203- else if ( field . DeclaringType . IsSerializable && ! field . IsNotSerialized )
218+ /*
219+ * System.Xml.Serialization.XmlSerializer
220+ *
221+ * XmlSerializer by default serializes fields marked with [NonSerialized]
222+ * This is a work-around that causes all fields in a class marked [Serializable]
223+ * to _not_ be renamed, unless marked with [XmlIgnoreAttribute]
224+ *
225+ * If we have a way to detect which serializer method the code is going to use
226+ * for the class, or if Microsoft makes XmlSerializer respond to [NonSerialized]
227+ * we'll have a more accurate way to achieve this.
228+ */
229+ else if ( field . DeclaringType . IsSerializable ) // && !field.IsNotSerialized)
204230 service . SetCanRename ( field , false ) ;
205231
232+ else if ( field . DeclaringType . IsSerializable && ( field . CustomAttributes . IsDefined ( "XmlIgnore" )
233+ || field . CustomAttributes . IsDefined ( "XmlIgnoreAttribute" )
234+ || field . CustomAttributes . IsDefined ( "System.Xml.Serialization.XmlIgnore" )
235+ || field . CustomAttributes . IsDefined ( "System.Xml.Serialization.XmlIgnoreAttribute" )
236+ || field . CustomAttributes . IsDefined ( "T:System.Xml.Serialization.XmlIgnoreAttribute" ) ) ) // Can't seem to detect CustomAttribute
237+ service . SetCanRename ( field , true ) ;
238+ /*
239+ * End of XmlSerializer work-around
240+ */
241+
206242 else if ( field . IsLiteral && field . DeclaringType . IsEnum &&
207243 ! parameters . GetParameter ( context , field , "renEnum" , false ) )
208244 service . SetCanRename ( field , false ) ;
0 commit comments