Skip to content

Commit

Permalink
Merge pull request #465 from Dewera/master
Browse files Browse the repository at this point in the history
Add a non generic overload to ForDestinationType
  • Loading branch information
andrerav committed Sep 5, 2022
2 parents fb325ef + c3f862f commit ebfa938
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Mapster/TypeAdapterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ public TypeAdapterSetter<TDestination> ForDestinationType<TDestination>()
return new TypeAdapterSetter<TDestination>(settings, this);
}

public TypeAdapterSetter ForDestinationType(Type destinationType)
{
var key = new TypeTuple(typeof(void), destinationType);
var settings = GetSettings(key);
return new TypeAdapterSetter(settings, this);
}

private TypeAdapterSettings GetSettings(TypeTuple key)
{
var rule = this.RuleMap.GetOrAdd(key, types =>
Expand Down Expand Up @@ -212,7 +219,7 @@ private static TypeAdapterRule CreateDestinationTypeRule(TypeTuple key)
score--;
type1 = type1.GetTypeInfo().BaseType;
}
return type1 != null && type1.GetTypeInfo().IsGenericType && type1.GetGenericTypeDefinition() == type2
return type1 != null && type1.GetTypeInfo().IsGenericType && type1.GetGenericTypeDefinition() == type2
? (int?)score
: null;
}
Expand Down Expand Up @@ -271,7 +278,7 @@ public Func<TSource, TDestination, TDestination> GetMapToTargetFunction<TSource,
internal Delegate GetMapToTargetFunction(Type sourceType, Type destinationType)
{
var key = new TypeTuple(sourceType, destinationType);
if (!_mapToTargetDict.TryGetValue(key, out var del))
if (!_mapToTargetDict.TryGetValue(key, out var del))
del = AddToHash(_mapToTargetDict, key, tuple => Compiler(CreateMapExpression(tuple, MapType.MapToTarget)));
return del;
}
Expand All @@ -295,7 +302,7 @@ internal MethodCallExpression GetProjectionCallExpression(Type sourceType, Type
public Func<object, TDestination> GetDynamicMapFunction<TDestination>(Type sourceType)
{
var key = new TypeTuple(sourceType, typeof(TDestination));
if (!_dynamicMapDict.TryGetValue(key, out var del))
if (!_dynamicMapDict.TryGetValue(key, out var del))
del = AddToHash(_dynamicMapDict, key, tuple => Compiler(CreateDynamicMapExpression(tuple)));
return (Func<object, TDestination>)del;
}
Expand Down Expand Up @@ -700,7 +707,7 @@ private void Remove(TypeTuple key)
return config;
});
public TypeAdapterConfig Clone()
{
{
var fn = _cloneConfig.Value.GetMapFunction<TypeAdapterConfig, TypeAdapterConfig>();
return fn(this);
}
Expand Down

0 comments on commit ebfa938

Please sign in to comment.