diff --git a/src/TemplateTest/CreateMapExpressionTest.cs b/src/TemplateTest/CreateMapExpressionTest.cs index 514229aa..4a1d827a 100644 --- a/src/TemplateTest/CreateMapExpressionTest.cs +++ b/src/TemplateTest/CreateMapExpressionTest.cs @@ -5,7 +5,10 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; +using System.Net.Security; +using System.Reflection; namespace TemplateTest { @@ -85,13 +88,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; @@ -109,11 +113,14 @@ public void TestRegressionMapperGenerationTranslation() expr, ExpressionTranslator.LambdaType.PublicMethod, typeof(IMyTypeMapper), - method.Name + method.Name, + !method.IsPublic ); } - 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; @@ -131,25 +138,102 @@ public void TestRegressionMapperGenerationTranslation() expr, ExpressionTranslator.LambdaType.PublicLambda, typeof(IMyTypeMapper), - prop.Name + prop.Name, + !prop.GetMethod?.IsPublic ?? false ); } 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 @@ -172,10 +256,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