@@ -269,10 +269,30 @@ public static TSetter UseDestinationValue<TSetter>(this TSetter setter, Func<IMe
269269 return setter ;
270270 }
271271
272- internal static TSetter Include < TSetter > ( this TSetter setter , Type sourceType , Type destType ) where TSetter : TypeAdapterSetter
272+ public static TSetter Include < TSetter > ( this TSetter setter , Type sourceType , Type destType ) where TSetter : TypeAdapterSetter
273273 {
274274 setter . CheckCompiled ( ) ;
275275
276+ Type baseSourceType = setter . Config . SourceType ;
277+ Type baseDestinationType = setter . Config . DestinationType ;
278+
279+ if ( baseSourceType . IsOpenGenericType ( ) && baseDestinationType . IsOpenGenericType ( ) )
280+ {
281+ if ( ! sourceType . IsAssignableToGenericType ( baseSourceType ) )
282+ throw new InvalidCastException ( "In order to use inherits, TSource must be inherited from TBaseSource." ) ;
283+ if ( ! destType . IsAssignableToGenericType ( baseDestinationType ) )
284+ throw new InvalidCastException ( "In order to use inherits, TDestination must be inherited from TBaseDestination." ) ;
285+ }
286+ else
287+ {
288+ if ( ! baseSourceType . GetTypeInfo ( ) . IsAssignableFrom ( sourceType . GetTypeInfo ( ) ) )
289+ throw new InvalidCastException ( "In order to use inherits, TSource must be inherited from TBaseSource." ) ;
290+
291+ if ( ! baseDestinationType . GetTypeInfo ( ) . IsAssignableFrom ( destType . GetTypeInfo ( ) ) )
292+ throw new InvalidCastException ( "In order to use inherits, TDestination must be inherited from TBaseDestination." ) ;
293+ }
294+
295+
276296 setter . Config . Rules . LockAdd ( new TypeAdapterRule
277297 {
278298 Priority = arg =>
@@ -286,6 +306,36 @@ internal static TSetter Include<TSetter>(this TSetter setter, Type sourceType, T
286306 return setter ;
287307 }
288308
309+ public static TSetter Inherits < TSetter > ( this TSetter setter , Type baseSourceType , Type baseDestinationType ) where TSetter : TypeAdapterSetter
310+ {
311+ setter . CheckCompiled ( ) ;
312+
313+ Type derivedSourceType = setter . Config . SourceType ;
314+ Type derivedDestinationType = setter . Config . DestinationType ;
315+
316+ if ( baseSourceType . IsOpenGenericType ( ) && baseDestinationType . IsOpenGenericType ( ) )
317+ {
318+ if ( ! derivedSourceType . IsAssignableToGenericType ( baseSourceType ) )
319+ throw new InvalidCastException ( "In order to use inherits, TSource must be inherited from TBaseSource." ) ;
320+ if ( ! derivedDestinationType . IsAssignableToGenericType ( baseDestinationType ) )
321+ throw new InvalidCastException ( "In order to use inherits, TDestination must be inherited from TBaseDestination." ) ;
322+ }
323+ else
324+ {
325+ if ( ! baseSourceType . GetTypeInfo ( ) . IsAssignableFrom ( derivedSourceType . GetTypeInfo ( ) ) )
326+ throw new InvalidCastException ( "In order to use inherits, TSource must be inherited from TBaseSource." ) ;
327+
328+ if ( ! baseDestinationType . GetTypeInfo ( ) . IsAssignableFrom ( derivedDestinationType . GetTypeInfo ( ) ) )
329+ throw new InvalidCastException ( "In order to use inherits, TDestination must be inherited from TBaseDestination." ) ;
330+ }
331+
332+ if ( setter . Config . RuleMap . TryGetValue ( new TypeTuple ( baseSourceType , baseDestinationType ) , out var rule ) )
333+ {
334+ setter . Settings . Apply ( rule . Settings ) ;
335+ }
336+ return setter ;
337+ }
338+
289339 public static TSetter ApplyAdaptAttribute < TSetter > ( this TSetter setter , BaseAdaptAttribute attr ) where TSetter : TypeAdapterSetter
290340 {
291341 if ( attr . IgnoreAttributes != null )
@@ -812,20 +862,8 @@ public TypeAdapterSetter<TSource, TDestination> Inherits<TBaseSource, TBaseDesti
812862 {
813863 this . CheckCompiled ( ) ;
814864
815- Type baseSourceType = typeof ( TBaseSource ) ;
816- Type baseDestinationType = typeof ( TBaseDestination ) ;
817-
818- if ( ! baseSourceType . GetTypeInfo ( ) . IsAssignableFrom ( typeof ( TSource ) . GetTypeInfo ( ) ) )
819- throw new InvalidCastException ( "In order to use inherits, TSource must be inherited from TBaseSource." ) ;
820-
821- if ( ! baseDestinationType . GetTypeInfo ( ) . IsAssignableFrom ( typeof ( TDestination ) . GetTypeInfo ( ) ) )
822- throw new InvalidCastException ( "In order to use inherits, TDestination must be inherited from TBaseDestination." ) ;
865+ return this . Inherits ( typeof ( TBaseSource ) , typeof ( TBaseDestination ) ) ;
823866
824- if ( Config . RuleMap . TryGetValue ( new TypeTuple ( baseSourceType , baseDestinationType ) , out var rule ) )
825- {
826- Settings . Apply ( rule . Settings ) ;
827- }
828- return this ;
829867 }
830868
831869 public TypeAdapterSetter < TSource , TDestination > Fork ( Action < TypeAdapterConfig > action )
0 commit comments