Skip to content

Commit 531f0ed

Browse files
committed
add RecordType identity helper
add targeting to RecordType Clone Method
1 parent 62ab7d6 commit 531f0ed

3 files changed

Lines changed: 53 additions & 12 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Mapster.Utils
9+
{
10+
/// <summary>
11+
/// CheckTools from Distinctive features of RecordType according to specification:
12+
/// https://github.com/dotnet/docs/blob/main/docs/csharp/language-reference/builtin-types/record.md
13+
/// </summary>
14+
public static class RecordTypeIdentityHelper
15+
{
16+
private static bool IsRecordСonstructor(Type type)
17+
{
18+
var ctors = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).ToList();
19+
20+
var isRecordTypeCtor = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)
21+
.Where(x => x.IsFamily == true || (type.IsSealed && x.IsPrivate == true)) // add target from Sealed record
22+
.Any(x => x.GetParameters()
23+
.Any(y => y.ParameterType == type));
24+
25+
26+
if (ctors.Count >= 2 && isRecordTypeCtor)
27+
return true;
28+
29+
30+
return false;
31+
32+
}
33+
34+
private static bool IsIncludedRecordCloneMethod(Type type)
35+
{
36+
if( type.GetMethod("<Clone>$")?.MethodImplementationFlags.HasFlag(MethodImplAttributes.IL) == true)
37+
return true;
38+
39+
return false;
40+
}
41+
42+
public static bool IsRecordType(Type type)
43+
{
44+
if (IsRecordСonstructor(type) && IsIncludedRecordCloneMethod(type))
45+
return true;
46+
47+
48+
return false;
49+
}
50+
51+
}
52+
}

src/Mapster.Tests/WhenMappingRecordRegression.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ public void ImplicitOperatorCurrentWorkFromClass()
243243
#region NowNotWorking
244244

245245
[TestMethod]
246-
[Ignore]
247246
public void DetectFakeRecord()
248247
{
249248
var _source = new TestClassPublicCtr(200);

src/Mapster/Utils/ReflectionUtils.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,20 +205,10 @@ public static bool IsRecordType(this Type type)
205205
props.Any(p => p.SetterModifier != AccessModifier.Public))
206206
return true;
207207

208-
//constructor
209-
var ctors = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).ToList();
210-
211-
var isRecordTypeCtor = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)
212-
.Where(x => x.IsFamily == true || (type.Attributes.HasFlag(TypeAttributes.Sealed) && x.IsPrivate == true)) // add target from Sealed record
213-
.Any(x => x.GetParameters()
214-
.Any(y => y.ParameterType == type));
215-
216-
217-
if (ctors.Count >= 2 && isRecordTypeCtor)
208+
if(RecordTypeIdentityHelper.IsRecordType(type))
218209
return true;
219210

220211

221-
222212
return false;
223213
}
224214

0 commit comments

Comments
 (0)