From 2d206257f9cbda223898a0a4d3338b791456df20 Mon Sep 17 00:00:00 2001 From: DocSvartz Date: Thu, 20 Aug 2026 17:10:44 +0500 Subject: [PATCH 1/2] add internal omly --- src/TemplateTest/CreateMapExpressionTest.cs | 101 +++++++++++++++++--- 1 file changed, 90 insertions(+), 11 deletions(-) diff --git a/src/TemplateTest/CreateMapExpressionTest.cs b/src/TemplateTest/CreateMapExpressionTest.cs index 514229aa..2228b961 100644 --- a/src/TemplateTest/CreateMapExpressionTest.cs +++ b/src/TemplateTest/CreateMapExpressionTest.cs @@ -5,7 +5,9 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; +using System.Reflection; namespace TemplateTest { @@ -85,13 +87,14 @@ public void TestRegressionMapperGenerationTranslation() Implements = new[] { typeof(IMyTypeMapper) }, Namespace = "Benchmark", TypeName = "CustomerMapper", - IsInternal = true, + IsInternal = false, GeneratedAttributes = new(new[] {new MapsterToolGeneratedMapperAttribute()}) }; var translator = new ExpressionTranslator(definitions); - foreach (var method in typeof(IMyTypeMapper).GetMethods()) + foreach (var method in typeof(IMyTypeMapper).GetMethods(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance) + .Where(x=>x.IsPublicOrInternal())) { if (method.IsGenericMethod) continue; @@ -113,7 +116,9 @@ public void TestRegressionMapperGenerationTranslation() ); } - foreach (var prop in typeof(IMyTypeMapper).GetProperties()) + foreach (var prop in typeof(IMyTypeMapper).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) + .Where(x=>x.IsGetterPublicOrInternal()) + ) { if (!prop.PropertyType.IsGenericType) continue; @@ -138,21 +143,95 @@ public void TestRegressionMapperGenerationTranslation() var txt = translator.ToString(); + var src = new Address() { City = "City 17"}; + + IMyTypeMapper mapper = new CustomerMapper(); + + var result = mapper.Map(src); + + Assert.IsTrue(txt.Contains("Expression> TemplateTest.IMyTypeMapper.Projection")); Assert.IsTrue(txt.Contains("AddressDTO TemplateTest.IMyTypeMapper.Map")); Assert.IsTrue(txt.Contains("[MapsterToolGeneratedMapper]")); + } + + } + + public partial class CustomerMapper : IMyTypeMapper + { + + AddressDTO TemplateTest.IMyTypeMapper.Map(Address p1) + { + return p1 == null ? null : new AddressDTO() + { + Id = p1.Id, + City = p1.City, + Country = p1.Country + }; + } + + Expression> TemplateTest.IMyTypeMapper.Projection => p2 => new Address() + { + Id = p2.Id, + City = p2.City, + Country = p2.Country + }; + } + + + + + public static class MethodInfoExtensions + { + public static bool IsPublicOrInternal(this MethodInfo method) + { + if (method == null) throw new ArgumentNullException(nameof(method)); + return !method.IsPrivate + && !method.IsFamily + && !method.IsFamilyOrAssembly + && !method.IsFamilyAndAssembly + && (method.IsPublic || true); } + + + public static bool IsGetterPublicOrInternal(this PropertyInfo property) + { + if (property == null) throw new ArgumentNullException(nameof(property)); + + MethodInfo? getMethod = property.GetMethod; + + if (getMethod == null) return false; + + return !getMethod.IsPrivate + && !getMethod.IsFamily + && !getMethod.IsFamilyOrAssembly + && !getMethod.IsFamilyAndAssembly + && (getMethod.IsPublic || true); + } } - internal interface IMyTypeMapper + + + + + + + + + + + + + + public interface IMyTypeMapper { - AddressDTO Map(Address p1); - Expression> Projection { get; } + internal AddressDTO Map(Address p1); + internal Expression> Projection { get; } } - public class Address + internal class Address { public int Id { get; set; } public string Street { get; set; } @@ -172,10 +251,10 @@ public class Customer public int Id { get; set; } public string Name { get; set; } public decimal? Credit { get; set; } - public Address Address { get; set; } - public Address HomeAddress { get; set; } - public Address[] Addresses { get; set; } - public ICollection
WorkAddresses { get; set; } + //public Address Address { get; set; } + //public Address HomeAddress { get; set; } + //public Address[] Addresses { get; set; } + //public ICollection
WorkAddresses { get; set; } } public class CustomerDTO From cf77ad80066cf91c8b98c1ecb0bd5f1502af0c44 Mon Sep 17 00:00:00 2001 From: DocSvartz Date: Thu, 20 Aug 2026 20:49:40 +0500 Subject: [PATCH 2/2] fix access level --- src/TemplateTest/CreateMapExpressionTest.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/TemplateTest/CreateMapExpressionTest.cs b/src/TemplateTest/CreateMapExpressionTest.cs index 2228b961..4a1d827a 100644 --- a/src/TemplateTest/CreateMapExpressionTest.cs +++ b/src/TemplateTest/CreateMapExpressionTest.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Net.Security; using System.Reflection; namespace TemplateTest @@ -112,7 +113,8 @@ public void TestRegressionMapperGenerationTranslation() expr, ExpressionTranslator.LambdaType.PublicMethod, typeof(IMyTypeMapper), - method.Name + method.Name, + !method.IsPublic ); } @@ -136,7 +138,8 @@ public void TestRegressionMapperGenerationTranslation() expr, ExpressionTranslator.LambdaType.PublicLambda, typeof(IMyTypeMapper), - prop.Name + prop.Name, + !prop.GetMethod?.IsPublic ?? false ); } @@ -157,6 +160,8 @@ public void TestRegressionMapperGenerationTranslation() } + + public partial class CustomerMapper : IMyTypeMapper { @@ -231,7 +236,7 @@ public interface IMyTypeMapper internal Expression> Projection { get; } } - internal class Address + public class Address { public int Id { get; set; } public string Street { get; set; }