diff --git a/src/Mapster.Tests/WhenMappingRecordRegression.cs b/src/Mapster.Tests/WhenMappingRecordRegression.cs index bcf0c96e..1cf00d11 100644 --- a/src/Mapster.Tests/WhenMappingRecordRegression.cs +++ b/src/Mapster.Tests/WhenMappingRecordRegression.cs @@ -2,6 +2,7 @@ using Shouldly; using System; using System.Collections.Generic; +using System.Globalization; using System.Text.Json; using static Mapster.Tests.WhenExplicitMappingRequired; using static Mapster.Tests.WhenMappingDerived; @@ -562,6 +563,23 @@ public void NotSelfCreationTypeMappingInContainingClassWithoutError() uriDest.Uri.ToString().ShouldBe("https://www.google.com/"); } + /// + /// https://github.com/MapsterMapper/Mapster/issues/995 + /// + [TestMethod] + public void TypeWithOutPublicCtorDetectAsNotSelfCreationType() + { + var src = new CultureInfo("fr-FR"); + + Should.NotThrow(()=> + { + //src.Adapt(); + src.TextInfo.Adapt(); + }); + + } + + class SourceClassWithJsonDocument911 { public required JsonDocument Json { get; init; } diff --git a/src/Mapster/Utils/ReflectionUtils.cs b/src/Mapster/Utils/ReflectionUtils.cs index 36a792fc..8203858f 100644 --- a/src/Mapster/Utils/ReflectionUtils.cs +++ b/src/Mapster/Utils/ReflectionUtils.cs @@ -460,9 +460,11 @@ public static bool IsNotSelfCreation(this Type type) return false; if(type.IsCollectionCompatible()) return false; - + if (type.GetConstructors().Length == 0) + return true; if (type == typeof(Type) || type.BaseType == typeof(MulticastDelegate)) return true; + return type.GetFieldsAndProperties().All(it => (it.SetterModifier & (AccessModifier.Public | AccessModifier.NonPublic)) == 0); }