@@ -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 ) ;
@@ -219,7 +255,31 @@ void Analyze(NameService service, ConfuserContext context, ProtectionParameters
219255 else if ( parameters . GetParameter ( context , property , "forceRen" , false ) )
220256 return ;
221257
222- else if ( property . DeclaringType . Implements ( "System.ComponentModel.INotifyPropertyChanged" ) )
258+ /*
259+ * System.Xml.Serialization.XmlSerializer
260+ *
261+ * XmlSerializer by default serializes fields marked with [NonSerialized]
262+ * This is a work-around that causes all fields in a class marked [Serializable]
263+ * to _not_ be renamed, unless marked with [XmlIgnoreAttribute]
264+ *
265+ * If we have a way to detect which serializer method the code is going to use
266+ * for the class, or if Microsoft makes XmlSerializer respond to [NonSerialized]
267+ * we'll have a more accurate way to achieve this.
268+ */
269+ else if ( property . DeclaringType . IsSerializable ) // && !field.IsNotSerialized)
270+ service . SetCanRename ( property , false ) ;
271+
272+ else if ( property . DeclaringType . IsSerializable && ( property . CustomAttributes . IsDefined ( "XmlIgnore" )
273+ || property . CustomAttributes . IsDefined ( "XmlIgnoreAttribute" )
274+ || property . CustomAttributes . IsDefined ( "System.Xml.Serialization.XmlIgnore" )
275+ || property . CustomAttributes . IsDefined ( "System.Xml.Serialization.XmlIgnoreAttribute" )
276+ || property . CustomAttributes . IsDefined ( "T:System.Xml.Serialization.XmlIgnoreAttribute" ) ) ) // Can't seem to detect CustomAttribute
277+ service . SetCanRename ( property , true ) ;
278+ /*
279+ * End of XmlSerializer work-around
280+ */
281+
282+ else if ( property . DeclaringType . Implements ( "System.ComponentModel.INotifyPropertyChanged" ) )
223283 service . SetCanRename ( property , false ) ;
224284
225285 else if ( property . DeclaringType . Name . String . Contains ( "AnonymousType" ) )
0 commit comments