diff --git a/src/Mapster/TypeAdapterConfig.cs b/src/Mapster/TypeAdapterConfig.cs index a6633bb0..1e6abade 100644 --- a/src/Mapster/TypeAdapterConfig.cs +++ b/src/Mapster/TypeAdapterConfig.cs @@ -13,8 +13,6 @@ namespace Mapster { public class TypeAdapterConfig { - public Type SourceType { get; protected set; } - public Type DestinationType { get; protected set; } public static List RulesTemplate { get; } = CreateRuleTemplate(); public static TypeAdapterConfig GlobalSettings { get; } = new TypeAdapterConfig(); @@ -150,9 +148,6 @@ public TypeAdapterSetter When(Func canMap) /// public TypeAdapterSetter NewConfig() { - this.SourceType = typeof(TSource); - this.DestinationType = typeof(TDestination); - Remove(typeof(TSource), typeof(TDestination)); return ForType(); } @@ -166,9 +161,6 @@ public TypeAdapterSetter NewConfig /// public TypeAdapterSetter NewConfig(Type sourceType, Type destinationType) { - this.SourceType = sourceType; - this.DestinationType = destinationType; - Remove(sourceType, destinationType); return ForType(sourceType, destinationType); } @@ -182,9 +174,6 @@ public TypeAdapterSetter NewConfig(Type sourceType, Type destinationType) /// public TypeAdapterSetter ForType() { - this.SourceType = typeof(TSource); - this.DestinationType = typeof(TDestination); - var key = new TypeTuple(typeof(TSource), typeof(TDestination)); var settings = GetSettings(key); return new TypeAdapterSetter(settings, this); @@ -199,9 +188,6 @@ public TypeAdapterSetter ForType() /// public TypeAdapterSetter ForType(Type sourceType, Type destinationType) { - this.SourceType = sourceType; - this.DestinationType = destinationType; - var key = new TypeTuple(sourceType, destinationType); var settings = GetSettings(key); return new TypeAdapterSetter(settings, this); @@ -215,9 +201,6 @@ public TypeAdapterSetter ForType(Type sourceType, Type destinationType) /// public TypeAdapterSetter ForDestinationType() { - this.SourceType = typeof(void); - this.DestinationType = typeof(TDestination); - var key = new TypeTuple(typeof(void), typeof(TDestination)); var settings = GetSettings(key); return new TypeAdapterSetter(settings, this); @@ -231,9 +214,6 @@ public TypeAdapterSetter ForDestinationType() /// public TypeAdapterSetter ForDestinationType(Type destinationType) { - this.SourceType = typeof(void); - this.DestinationType = destinationType; - var key = new TypeTuple(typeof(void), destinationType); var settings = GetSettings(key); return new TypeAdapterSetter(settings, this); @@ -249,6 +229,10 @@ private TypeAdapterSettings GetSettings(TypeTuple key) Rules.LockAdd(r); return r; }); + + rule.Settings.SourceType = key.Source; + rule.Settings.DestinationType = key.Destination; + return rule.Settings; } diff --git a/src/Mapster/TypeAdapterSetter.cs b/src/Mapster/TypeAdapterSetter.cs index 11820954..e08ef312 100644 --- a/src/Mapster/TypeAdapterSetter.cs +++ b/src/Mapster/TypeAdapterSetter.cs @@ -1,10 +1,10 @@ -using System; +using Mapster.Adapters; +using Mapster.Models; +using Mapster.Utils; +using System; using System.Linq; using System.Linq.Expressions; using System.Reflection; -using Mapster.Adapters; -using Mapster.Models; -using Mapster.Utils; namespace Mapster { @@ -273,8 +273,8 @@ public static TSetter Include(this TSetter setter, Type sourceType, Typ { setter.CheckCompiled(); - Type baseSourceType = setter.Config.SourceType; - Type baseDestinationType = setter.Config.DestinationType; + Type baseSourceType = setter.Settings.SourceType ?? typeof(void); + Type baseDestinationType = setter.Settings.DestinationType ?? typeof(void); if (baseSourceType.IsOpenGenericType() && baseDestinationType.IsOpenGenericType()) { @@ -291,8 +291,7 @@ public static TSetter Include(this TSetter setter, Type sourceType, Typ if (!baseDestinationType.GetTypeInfo().IsAssignableFrom(destType.GetTypeInfo())) throw new InvalidCastException("In order to use inherits, TDestination must be inherited from TBaseDestination."); } - - + setter.Config.Rules.LockAdd(new TypeAdapterRule { Priority = arg => @@ -310,8 +309,8 @@ public static TSetter Inherits(this TSetter setter, Type baseSourceType { setter.CheckCompiled(); - Type derivedSourceType = setter.Config.SourceType; - Type derivedDestinationType = setter.Config.DestinationType; + Type derivedSourceType = setter.Settings.SourceType ?? typeof(void); + Type derivedDestinationType = setter.Settings.DestinationType ?? typeof(void); if(baseSourceType.IsOpenGenericType() && baseDestinationType.IsOpenGenericType()) { diff --git a/src/Mapster/TypeAdapterSettings.cs b/src/Mapster/TypeAdapterSettings.cs index c896536f..15c666a2 100644 --- a/src/Mapster/TypeAdapterSettings.cs +++ b/src/Mapster/TypeAdapterSettings.cs @@ -10,6 +10,16 @@ namespace Mapster [AdaptWith(AdaptDirectives.DestinationAsRecord)] public class TypeAdapterSettings : SettingStore { + public Type? SourceType + { + get => Get(nameof(SourceType)); + set => Set(nameof(SourceType), value); + } + public Type? DestinationType + { + get => Get(nameof(DestinationType)); + set => Set(nameof(DestinationType), value); + } public IgnoreDictionary Ignore { get => Get(nameof(Ignore), () => new IgnoreDictionary());