From 519ab611368ae5994010760e765490dc890430b8 Mon Sep 17 00:00:00 2001 From: DocSvartz Date: Mon, 6 Jan 2025 09:03:07 +0500 Subject: [PATCH 1/5] add ignore filter to DynamicTypeGenerator --- src/Mapster/Utils/DynamicTypeGenerator.cs | 37 +++++++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Mapster/Utils/DynamicTypeGenerator.cs b/src/Mapster/Utils/DynamicTypeGenerator.cs index 7e901417..517d4adf 100644 --- a/src/Mapster/Utils/DynamicTypeGenerator.cs +++ b/src/Mapster/Utils/DynamicTypeGenerator.cs @@ -22,6 +22,16 @@ internal static class DynamicTypeGenerator private static readonly ConcurrentDictionary _generated = new ConcurrentDictionary(); 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 @@ -86,13 +96,14 @@ private static Type CreateTypeForInterface(Type interfaceType) if (hasReadonlyProps) { + var filterargs = DropIgnorMemebers(args); var ctorBuilder = builder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, - args.Select(it => it.FieldType).ToArray()); + filterargs.Select(it => it.FieldType).ToArray()); var ctorIl = ctorBuilder.GetILGenerator(); - for (var i = 0; i < args.Count; i++) + for (var i = 0; i < filterargs.Count; i++) { - var arg = args[i]; + var arg = filterargs[i]; ctorBuilder.DefineParameter(i + 1, ParameterAttributes.None, arg.Name.Substring(1)); ctorIl.Emit(OpCodes.Ldarg_0); ctorIl.Emit(OpCodes.Ldarg_S, i + 1); @@ -175,5 +186,25 @@ private static void CreateMethod(TypeBuilder builder, MethodInfo interfaceMethod builder.DefineMethodOverride(classMethod, interfaceMethod); } + + private static List DropIgnorMemebers(List fields) + { + var ignorFields = ignoreMembers.Select(x => x.Key).ToArray(); + List filtered = new List(); + + + foreach (var item in fields) + { + foreach (var check in ignorFields) + { + if (item.Name != $"_{MapsterHelper.CamelCase(check)}") + { + filtered.Add(item); + } + } + } + + return filtered; + } } } From 4654885127a41f3fb15f7a1f9f415913c2047f66 Mon Sep 17 00:00:00 2001 From: DocSvartz Date: Mon, 6 Jan 2025 09:04:42 +0500 Subject: [PATCH 2/5] replaxe to ignore mathing method --- src/Mapster/Adapters/ReadOnlyInterfaceAdapter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mapster/Adapters/ReadOnlyInterfaceAdapter.cs b/src/Mapster/Adapters/ReadOnlyInterfaceAdapter.cs index df4a0f66..160767ca 100644 --- a/src/Mapster/Adapters/ReadOnlyInterfaceAdapter.cs +++ b/src/Mapster/Adapters/ReadOnlyInterfaceAdapter.cs @@ -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]; From 760e2a6c34edabc5a02e1ba56829fce162389aea Mon Sep 17 00:00:00 2001 From: DocSvartz Date: Mon, 6 Jan 2025 09:23:03 +0500 Subject: [PATCH 3/5] add Tests --- src/Mapster.Tests/WhenMappingToInterface.cs | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/Mapster.Tests/WhenMappingToInterface.cs b/src/Mapster.Tests/WhenMappingToInterface.cs index 5e6f0358..7c15cd17 100644 --- a/src/Mapster.Tests/WhenMappingToInterface.cs +++ b/src/Mapster.Tests/WhenMappingToInterface.cs @@ -266,6 +266,41 @@ public void MappingToInteraceWithReadonlyProps_AllPropsInitialized() ); } + [TestMethod] + public void MappingToInteraceWithIgnorePrivateSetProperty() + { + TypeAdapterConfig + .NewConfig() + .TwoWays() + .Ignore(dest => dest.Ignore); + + + InterfaceDestination723 data = new Data723() { Inter = "234", Ignore = "00" }; + InterfaceSource723 datas = new Data723() { Inter = "150", Ignore = "10" }; + + var idestination = data.Adapt(); + var isourse = data.Adapt(); + + var result = datas.Adapt(data); + } + + public interface InterfaceDestination723 : InterfaceSource723 + { + public string Ignore { get; } + } + + public interface InterfaceSource723 + { + public string Inter { get; set; } + } + + private class Data723 : InterfaceDestination723 + { + public string Ignore { get; set; } + + public string Inter { get; set; } + } + public interface IInheritedDtoWithoutProperties : IInheritedDto { } From 2db8380cebced0653263d8eff69623e09f4d6553 Mon Sep 17 00:00:00 2001 From: DocSvartz Date: Tue, 7 Jan 2025 06:24:17 +0500 Subject: [PATCH 4/5] Tests refactoring --- src/Mapster.Tests/WhenMappingToInterface.cs | 25 ++++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Mapster.Tests/WhenMappingToInterface.cs b/src/Mapster.Tests/WhenMappingToInterface.cs index 7c15cd17..c1261003 100644 --- a/src/Mapster.Tests/WhenMappingToInterface.cs +++ b/src/Mapster.Tests/WhenMappingToInterface.cs @@ -9,6 +9,8 @@ namespace Mapster.Tests [TestClass] public class WhenMappingToInterface { + + #region Tests [TestInitialize] public void Setup() { @@ -266,6 +268,9 @@ public void MappingToInteraceWithReadonlyProps_AllPropsInitialized() ); } + /// + /// https://github.com/MapsterMapper/Mapster/issues/723 + /// [TestMethod] public void MappingToInteraceWithIgnorePrivateSetProperty() { @@ -274,16 +279,24 @@ public void MappingToInteraceWithIgnorePrivateSetProperty() .TwoWays() .Ignore(dest => dest.Ignore); + InterfaceSource723 dataSourse = new Data723() { Inter = "IterDataSourse", Ignore = "IgnoreDataSourse" }; + InterfaceDestination723 dataDestination = new Data723() { Inter = "IterDataDestination", Ignore = "IgnoreDataDestination" }; + + var idestination = dataDestination.Adapt(); + var isourse = dataDestination.Adapt(); - InterfaceDestination723 data = new Data723() { Inter = "234", Ignore = "00" }; - InterfaceSource723 datas = new Data723() { Inter = "150", Ignore = "10" }; + var result = dataSourse.Adapt(dataDestination); - var idestination = data.Adapt(); - var isourse = data.Adapt(); + idestination.Ignore.ShouldBeNull(); // Not create initializer and param in constructor + idestination.Inter.ShouldBe("IterDataDestination"); - var result = datas.Adapt(data); + result.Ignore.ShouldBe("IgnoreDataDestination"); + result.Inter.ShouldBe("IterDataSourse"); } + #endregion Tests + + #region TestClasses public interface InterfaceDestination723 : InterfaceSource723 { public string Ignore { get; } @@ -409,6 +422,6 @@ public class PropertyInitializationTestSource public int Property1 { get; set; } public int Property2 { get; set; } } - + #endregion TestClasses } } From cb8c58e9cdaf000027f2151d72269c36f91a7906 Mon Sep 17 00:00:00 2001 From: DocSvartz Date: Tue, 7 Jan 2025 06:49:37 +0500 Subject: [PATCH 5/5] fix filter algorithm --- src/Mapster/Utils/DynamicTypeGenerator.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Mapster/Utils/DynamicTypeGenerator.cs b/src/Mapster/Utils/DynamicTypeGenerator.cs index 517d4adf..82d693bd 100644 --- a/src/Mapster/Utils/DynamicTypeGenerator.cs +++ b/src/Mapster/Utils/DynamicTypeGenerator.cs @@ -192,6 +192,8 @@ private static List DropIgnorMemebers(List fields) var ignorFields = ignoreMembers.Select(x => x.Key).ToArray(); List filtered = new List(); + if (ignorFields.Length == 0) + return fields; foreach (var item in fields) {