From 72bbba378364fe8c16eea6c224cd2039c1fc1a86 Mon Sep 17 00:00:00 2001 From: DocSvartz Date: Tue, 14 Jul 2026 09:06:41 +0500 Subject: [PATCH] fix: Detecting a type without public constructors like NotSelfCreation type --- .../WhenMappingRecordRegression.cs | 18 ++++++++++++++++++ src/Mapster/Utils/ReflectionUtils.cs | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) 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); }