Skip to content

Commit 7704405

Browse files
authored
chore: Merge pull request #998 from DocSvartz/fix-NonSelfCreation-Adapter
Detecting a type without public constructors as NotSelfCreation
2 parents 4c07701 + 72bbba3 commit 7704405

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/Mapster.Tests/WhenMappingRecordRegression.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Shouldly;
33
using System;
44
using System.Collections.Generic;
5+
using System.Globalization;
56
using System.Text.Json;
67
using static Mapster.Tests.WhenExplicitMappingRequired;
78
using static Mapster.Tests.WhenMappingDerived;
@@ -562,6 +563,23 @@ public void NotSelfCreationTypeMappingInContainingClassWithoutError()
562563
uriDest.Uri.ToString().ShouldBe("https://www.google.com/");
563564
}
564565

566+
/// <summary>
567+
/// https://github.com/MapsterMapper/Mapster/issues/995
568+
/// </summary>
569+
[TestMethod]
570+
public void TypeWithOutPublicCtorDetectAsNotSelfCreationType()
571+
{
572+
var src = new CultureInfo("fr-FR");
573+
574+
Should.NotThrow(()=>
575+
{
576+
//src.Adapt<CultureInfo>();
577+
src.TextInfo.Adapt<TextInfo>();
578+
});
579+
580+
}
581+
582+
565583
class SourceClassWithJsonDocument911
566584
{
567585
public required JsonDocument Json { get; init; }

src/Mapster/Utils/ReflectionUtils.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,11 @@ public static bool IsNotSelfCreation(this Type type)
460460
return false;
461461
if(type.IsCollectionCompatible())
462462
return false;
463-
463+
if (type.GetConstructors().Length == 0)
464+
return true;
464465
if (type == typeof(Type) || type.BaseType == typeof(MulticastDelegate))
465466
return true;
467+
466468

467469
return type.GetFieldsAndProperties().All(it => (it.SetterModifier & (AccessModifier.Public | AccessModifier.NonPublic)) == 0);
468470
}

0 commit comments

Comments
 (0)