Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative way Fix issue #723 : Ctor created with out Ignored member #761

Draft
wants to merge 4 commits into
base: development
Choose a base branch
from
Draft
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
40 changes: 40 additions & 0 deletions src/Mapster.Tests/WhenMappingToInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,46 @@ public void MappingToInterface_VerifyReadonlyPropsInterfaceRule()
);
}

/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/723
/// </summary>
[TestMethod]
public void MappingToIntefaceWithIgnorePrivateSetProperty()
{
TypeAdapterConfig<InterfaceSource723, InterfaceDestination723>
.NewConfig()
.TwoWays()
.Ignore(dest => dest.Ignore);

InterfaceDestination723 dataDestination = new Data723() { Inter = "IterDataDestination", Ignore = "IgnoreDataDestination" };

Should.NotThrow(() =>
{
var isourse = dataDestination.Adapt<InterfaceSource723>();
var idestination = dataDestination.Adapt<InterfaceDestination723>();
});

}

public interface InterfaceDestination723
{
public string Inter { get; set; }
public string Ignore { get; }
}

public interface InterfaceSource723
{
public string Inter { get; set; }
}

private class Data723 : InterfaceSource723, InterfaceDestination723
{
public string Ignore { get; set; }

public string Inter { get; set; }
}


public interface IInheritedDtoWithoutProperties : IInheritedDto
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/Adapters/ReadOnlyInterfaceAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override Expression CreateInstantiationExpression(Expression source, E
if (arg.GetConstructUsing() != null)
return base.CreateInstantiationExpression(source, destination, arg);

var destType = DynamicTypeGenerator.GetTypeForInterface(arg.DestinationType, arg.Settings.Includes.Count > 0);
var destType = DynamicTypeGenerator.GetTypeForInterface(arg.DestinationType, arg.Settings.Includes.Count > 0, arg.Settings.Ignore);
if (destType == null)
return base.CreateInstantiationExpression(source, destination, arg);
var ctor = destType.GetConstructors()[0];
Expand Down
41 changes: 36 additions & 5 deletions src/Mapster/Utils/DynamicTypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ internal static class DynamicTypeGenerator
private static readonly ConcurrentDictionary<Type, Type> _generated = new ConcurrentDictionary<Type, Type>();
private static int _generatedCounter;

private static IgnoreDictionary? ignoreMembers;

public static Type? GetTypeForInterface(Type interfaceType, bool ignoreError, IgnoreDictionary ignorMembers)
{
ignoreMembers = ignorMembers;

return GetTypeForInterface(interfaceType, ignoreError);

}

public static Type? GetTypeForInterface(Type interfaceType, bool ignoreError)
{
try
Expand Down Expand Up @@ -86,13 +96,14 @@ private static Type CreateTypeForInterface(Type interfaceType)

if (hasReadonlyProps)
{
var ctorBuilder = builder.DefineConstructor(MethodAttributes.Public,
var filteredArgs = DropIgnoredMemebers(args);
var ctorBuilder = builder.DefineConstructor(MethodAttributes.Public,
CallingConventions.Standard,
args.Select(it => it.FieldType).ToArray());
filteredArgs.Select(it => it.FieldType).ToArray());
var ctorIl = ctorBuilder.GetILGenerator();
for (var i = 0; i < args.Count; i++)
for (var i = 0; i < filteredArgs.Count; i++)
{
var arg = args[i];
var arg = filteredArgs[i];
ctorBuilder.DefineParameter(i + 1, ParameterAttributes.None, arg.Name.Substring(1));
ctorIl.Emit(OpCodes.Ldarg_0);
ctorIl.Emit(OpCodes.Ldarg_S, i + 1);
Expand Down Expand Up @@ -175,5 +186,25 @@ private static void CreateMethod(TypeBuilder builder, MethodInfo interfaceMethod

builder.DefineMethodOverride(classMethod, interfaceMethod);
}

private static List<FieldBuilder> DropIgnoredMemebers(List<FieldBuilder> fields)
{
if (ignoreMembers == null || ignoreMembers.Count == 0)
return fields;

var ignoreFields = ignoreMembers.Select(x => x.Key).ToArray();
var filtered = new List<FieldBuilder>();

foreach (var item in fields)
{
foreach (var check in ignoreFields)
{
if (item.Name != $"_{MapsterHelper.CamelCase(check)}")
filtered.Add(item);
}
}

return filtered;
}
}
}
}
Loading