@@ -410,7 +410,7 @@ private static Type ToClass(Type type)
410410 private static IList ReadJsonArray ( JToken token , Type type , int ? count = null )
411411 {
412412 var classType = ToClass ( type ) ;
413- var listType = typeof ( List < > ) . MakeGenericType ( classType ) ;
413+ var listType = typeof ( List < > ) . MakeGenericType ( type ) ; // always read into list of interfaces
414414 var list = Activator . CreateInstance ( listType ) ;
415415 var i = 0 ;
416416
@@ -425,25 +425,33 @@ private static IList ReadJsonArray(JToken token, Type type, int? count = null)
425425 var typeName = GetTypeNameFromToken ( childToken ) ;
426426 if ( string . IsNullOrEmpty ( typeName ) )
427427 {
428- var item = childToken . ToObject ( classType ) ;
429- listType
430- . GetRuntimeMethod ( nameof ( List < object > . Add ) , new [ ] { classType } )
431- . Invoke ( list , new object [ ] { item } ) ;
428+ var child = childToken . ToObject ( classType ) ;
429+ var method = listType . GetRuntimeMethod ( nameof ( List < object > . Add ) , new [ ] { classType } ) ;
430+
431+ if ( method != null )
432+ {
433+ method . Invoke ( list , new object [ ] { child } ) ;
434+
435+ i ++ ;
436+ }
432437 }
433438 else
434439 {
435440 var builtType = Type . GetType ( $ "{ NamespacePrefix } { typeName } ") ;
436- if ( builtType != null && GetTypeHierarchy ( builtType ) . Any ( x => x == classType ) )
441+ if ( builtType != null && type . GetTypeInfo ( ) . IsAssignableFrom ( builtType . GetTypeInfo ( ) ) )
437442 {
438443 var child = ( Thing ) childToken . ToObject ( builtType ) ;
439- listType
440- . GetRuntimeMethod ( nameof ( List < object > . Add ) , new [ ] { classType } )
441- . Invoke ( list , new object [ ] { child } ) ;
444+ var method = listType . GetRuntimeMethod ( nameof ( List < object > . Add ) , new [ ] { classType } ) ;
445+
446+ if ( method != null )
447+ {
448+ method . Invoke ( list , new object [ ] { child } ) ;
449+
450+ i ++ ;
451+ }
442452 }
443453 }
444454
445- i ++ ;
446-
447455 if ( i == count )
448456 {
449457 break ;
@@ -460,13 +468,18 @@ private static IEnumerable<Type> GetTypeHierarchy(Type type)
460468 yield break ;
461469 }
462470
463- yield return type ;
471+ var tt = type . GetTypeInfo ( ) . GetNestedTypes ( BindingFlags . Public ) ;
472+
473+ foreach ( var t in tt )
474+ {
475+ yield return t ;
476+ }
477+
478+ var ii = type . GetInterfaces ( ) ;
464479
465- var baseType = type . GetTypeInfo ( ) . BaseType ;
466- while ( baseType != null )
480+ foreach ( var i in ii )
467481 {
468- yield return baseType ;
469- baseType = baseType . GetTypeInfo ( ) . BaseType ;
482+ yield return i ;
470483 }
471484 }
472485
0 commit comments