-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #634 from DocSvartz/NewRecordDetecton
New Record detection - Adapt To Class not create new instance
- Loading branch information
Showing
6 changed files
with
549 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Mapster.Utils | ||
{ | ||
/// <summary> | ||
/// CheckTools from Distinctive features of RecordType according to specification: | ||
/// https://github.com/dotnet/docs/blob/main/docs/csharp/language-reference/builtin-types/record.md | ||
/// </summary> | ||
public static class RecordTypeIdentityHelper | ||
{ | ||
private static bool IsRecordСonstructor(Type type) | ||
{ | ||
var ctors = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).ToList(); | ||
|
||
if (ctors.Count < 2) | ||
return false; | ||
|
||
var isRecordTypeCtor = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic) | ||
.Where(x => x.IsFamily == true || (type.IsSealed && x.IsPrivate == true)) // add target from Sealed record | ||
.Any(x => x.GetParameters() | ||
.Any(y => y.ParameterType == type)); | ||
|
||
if (isRecordTypeCtor) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
private static bool IsIncludedRecordCloneMethod(Type type) | ||
{ | ||
if( type.GetMethod("<Clone>$")?.MethodImplementationFlags.HasFlag(MethodImplAttributes.IL) == true) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
public static bool IsRecordType(Type type) | ||
{ | ||
if (IsRecordСonstructor(type) && IsIncludedRecordCloneMethod(type)) | ||
return true; | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.