Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions src/Mapster/TypeAdapterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace Mapster
{
public class TypeAdapterConfig
{
public Type SourceType { get; protected set; }
public Type DestinationType { get; protected set; }
public static List<TypeAdapterRule> RulesTemplate { get; } = CreateRuleTemplate();
public static TypeAdapterConfig GlobalSettings { get; } = new TypeAdapterConfig();

Expand Down Expand Up @@ -150,9 +148,6 @@ public TypeAdapterSetter When(Func<PreCompileArgument, bool> canMap)
/// <returns></returns>
public TypeAdapterSetter<TSource, TDestination> NewConfig<TSource, TDestination>()
{
this.SourceType = typeof(TSource);
this.DestinationType = typeof(TDestination);

Remove(typeof(TSource), typeof(TDestination));
return ForType<TSource, TDestination>();
}
Expand All @@ -166,9 +161,6 @@ public TypeAdapterSetter<TSource, TDestination> NewConfig<TSource, TDestination>
/// <returns></returns>
public TypeAdapterSetter NewConfig(Type sourceType, Type destinationType)
{
this.SourceType = sourceType;
this.DestinationType = destinationType;

Remove(sourceType, destinationType);
return ForType(sourceType, destinationType);
}
Expand All @@ -182,9 +174,6 @@ public TypeAdapterSetter NewConfig(Type sourceType, Type destinationType)
/// <returns></returns>
public TypeAdapterSetter<TSource, TDestination> ForType<TSource, TDestination>()
{
this.SourceType = typeof(TSource);
this.DestinationType = typeof(TDestination);

var key = new TypeTuple(typeof(TSource), typeof(TDestination));
var settings = GetSettings(key);
return new TypeAdapterSetter<TSource, TDestination>(settings, this);
Expand All @@ -199,9 +188,6 @@ public TypeAdapterSetter<TSource, TDestination> ForType<TSource, TDestination>()
/// <returns></returns>
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);
Expand All @@ -215,9 +201,6 @@ public TypeAdapterSetter ForType(Type sourceType, Type destinationType)
/// <returns></returns>
public TypeAdapterSetter<TDestination> ForDestinationType<TDestination>()
{
this.SourceType = typeof(void);
this.DestinationType = typeof(TDestination);

var key = new TypeTuple(typeof(void), typeof(TDestination));
var settings = GetSettings(key);
return new TypeAdapterSetter<TDestination>(settings, this);
Expand All @@ -231,9 +214,6 @@ public TypeAdapterSetter<TDestination> ForDestinationType<TDestination>()
/// <returns></returns>
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);
Expand All @@ -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;
}

Expand Down
19 changes: 9 additions & 10 deletions src/Mapster/TypeAdapterSetter.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -273,8 +273,8 @@ public static TSetter Include<TSetter>(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())
{
Expand All @@ -291,8 +291,7 @@ public static TSetter Include<TSetter>(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 =>
Expand All @@ -310,8 +309,8 @@ public static TSetter Inherits<TSetter>(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())
{
Expand Down
10 changes: 10 additions & 0 deletions src/Mapster/TypeAdapterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ namespace Mapster
[AdaptWith(AdaptDirectives.DestinationAsRecord)]
public class TypeAdapterSettings : SettingStore
{
public Type? SourceType
{
get => Get<Type>(nameof(SourceType));
set => Set(nameof(SourceType), value);
}
public Type? DestinationType
{
get => Get<Type>(nameof(DestinationType));
set => Set(nameof(DestinationType), value);
}
public IgnoreDictionary Ignore
{
get => Get(nameof(Ignore), () => new IgnoreDictionary());
Expand Down
Loading