From a5cd9b3a679c45d6d8f0e4647c766bbdca7a076a Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Wed, 3 Jul 2024 12:40:47 -0700 Subject: [PATCH] Update XmlFormatter2.cs with setMethod handling and type comparison improvements x) Added setMethod handling for child elements x) Improved type comparison using IsAssignableFrom instead of IsSubclassOf --- ser/XmlFormatter2.cs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/ser/XmlFormatter2.cs b/ser/XmlFormatter2.cs index 7f27587..b9f13a1 100644 --- a/ser/XmlFormatter2.cs +++ b/ser/XmlFormatter2.cs @@ -648,14 +648,14 @@ namespace lib XmlElement childElem = getNamedChild( allChildren, name ); + var setMethod = childPi.GetSetMethod(); + if( childElem != null ) { object existingObj = childPi.GetValue( obj ); object childObj = Deserialize( childElem, childPi, childPi.PropertyType, existingObj ); - var setMethod = childPi.GetSetMethod(); - if( setMethod != null ) { //Object o = Activator.CreateInstance( setMethod.ReflectedType ); @@ -667,7 +667,24 @@ namespace lib { childPi.SetValue( obj, childObj ); } + } + else + { + object existingObj = childPi.GetValue( obj ); + object childObj = DeserializeConcrete( elem, childPi, name, childPi.PropertyType ); + + if( setMethod != null ) + { + //Object o = Activator.CreateInstance( setMethod.ReflectedType ); + setMethod.Invoke( obj, new object[] { childObj } ); + + //setMethod.CreateDelegate() + } + else + { + childPi.SetValue( obj, childObj ); + } } } } @@ -961,8 +978,9 @@ namespace lib { var existingObjType = existingObj.GetType(); - // @@@ GROSS Fix the types so theyre known good. - var isSubclass = type.IsSubclassOf( existingObjType ) || existingObjType.IsSubclassOf( type ); + //var isSubclass = type.IsSubclassOf( existingObjType ) || existingObjType.IsSubclassOf( type ); + var isSubclass = type.IsAssignableFrom( existingObjType ); + if( isSubclass ) {