Skip to content

Commit

Permalink
fix compile ForDestinationType
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaowlert committed Feb 12, 2019
1 parent 770a989 commit c2d96d9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/Mapster.Tests/WhenPerformingAfterMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public void After_Mapping_With_DestinationType_Setting()
result.IsValidated.ShouldBeTrue();
}


[TestMethod]
public void No_Compile_Error_When_ConstructUsing_ForDestinationType()
{
TypeAdapterConfig.GlobalSettings.ForDestinationType<IValidatable>()
.ConstructUsing(() => new SimpleDto());
TypeAdapterConfig.GlobalSettings.Compile();
}


public interface IValidatable
{
void Validate();
Expand Down
26 changes: 14 additions & 12 deletions src/Mapster/TypeAdapterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ private static List<TypeAdapterRule> CreateRuleTemplate()
Settings = new TypeAdapterSettings
{
//match exact name
NameMatchingStrategy = NameMatchingStrategy.Exact,
NameMatchingStrategy = NameMatchingStrategy.Exact,
ShouldMapMember =
{
ShouldMapMember.IgnoreAdaptIgnore, //ignore AdaptIgnore attribute
ShouldMapMember.AllowPublic, //match public prop
ShouldMapMember.AllowAdaptMember, //match AdaptMember attribute
},
GetMemberNames =
{
},
GetMemberNames =
{
GetMemberName.AdaptMember, //get name using AdaptMember attribute
},
ValueAccessingStrategies =
Expand Down Expand Up @@ -112,7 +112,7 @@ public TypeAdapterSetter When(Func<Type, Type, MapType, bool> canMap)
return new TypeAdapterSetter(rule.Settings, this);
}

public TypeAdapterSetter When(Func<PreCompileArgument, bool> canMap)
public TypeAdapterSetter When(Func<PreCompileArgument, bool> canMap)
{
var rule = new TypeAdapterRule
{
Expand Down Expand Up @@ -216,7 +216,7 @@ private static TypeAdapterRule CreateDestinationTypeRule(TypeTuple key)
score--;
type1 = type1.GetTypeInfo().BaseType;
}
return type1 == null ? null : (int?) score;
return type1 == null ? null : (int?)score;
}
if (!allowInheritance)
return null;
Expand Down Expand Up @@ -328,10 +328,10 @@ private MethodCallExpression CreateProjectionCallExpression(TypeTuple tuple)
var lambda = CreateMapExpression(tuple, MapType.Projection);
var source = Expression.Parameter(typeof(IQueryable<>).MakeGenericType(tuple.Source));
var methodInfo = (from method in typeof(Queryable).GetMethods()
where method.Name == nameof(Queryable.Select)
let p = method.GetParameters()[1]
where p.ParameterType.GetGenericArguments()[0].GetGenericTypeDefinition() == typeof(Func<,>)
select method).First().MakeGenericMethod(tuple.Source, tuple.Destination);
where method.Name == nameof(Queryable.Select)
let p = method.GetParameters()[1]
where p.ParameterType.GetGenericArguments()[0].GetGenericTypeDefinition() == typeof(Func<,>)
select method).First().MakeGenericMethod(tuple.Source, tuple.Destination);
return Expression.Call(methodInfo, source, Expression.Quote(lambda));
}

Expand All @@ -345,7 +345,7 @@ private static LambdaExpression CreateMapExpression(CompileArgument arg, bool al
if (allowNull)
return null;
else
throw new CompileException(arg,
throw new CompileException(arg,
new InvalidOperationException("ConverterFactory is not found"));
}
try
Expand Down Expand Up @@ -484,6 +484,8 @@ public void Compile()
var keys = RuleMap.Keys.ToList();
foreach (var key in keys)
{
if (key.Source == typeof(void))
continue;
_mapDict[key] = Compiler(CreateMapExpression(key, MapType.Map));
_mapToTargetDict[key] = Compiler(CreateMapExpression(key, MapType.MapToTarget));
}
Expand Down Expand Up @@ -608,7 +610,7 @@ public TypeAdapterConfig Clone()
if (config != null)
return (TypeAdapterConfig)config;

lock(_inlineConfigs)
lock (_inlineConfigs)
{
config = _inlineConfigs[key];
if (config != null)
Expand Down

0 comments on commit c2d96d9

Please sign in to comment.