Skip to content

Commit e7fa268

Browse files
committed
Implemented to also cover generic classes
1 parent 7b6a527 commit e7fa268

5 files changed

Lines changed: 19 additions & 10 deletions

File tree

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/TypeSearch/IIntrinsicTypePolicy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ namespace MackySoft.SerializeReferenceExtensions.Editor
44
{
55
public interface IIntrinsicTypePolicy
66
{
7-
bool IsAllowed (Type candiateType);
7+
bool IsAllowed (Type candiateType, bool ignoreGenericTypeCheck);
88
}
99
}

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/TypeSearch/IntrinsicTypePolicy/DefaultIntrinsicTypePolicy.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using UnityEngine;
23

34
namespace MackySoft.SerializeReferenceExtensions.Editor
45
{
@@ -7,12 +8,12 @@ public sealed class DefaultIntrinsicTypePolicy : IIntrinsicTypePolicy
78

89
public static readonly DefaultIntrinsicTypePolicy Instance = new DefaultIntrinsicTypePolicy();
910

10-
public bool IsAllowed (Type candiateType)
11+
public bool IsAllowed (Type candiateType, bool ignoreGenericTypeCheck)
1112
{
1213
return
1314
(candiateType.IsPublic || candiateType.IsNestedPublic || candiateType.IsNestedPrivate) &&
1415
!candiateType.IsAbstract &&
15-
!candiateType.IsGenericType &&
16+
(ignoreGenericTypeCheck || !candiateType.IsGenericType) &&
1617
!candiateType.IsPrimitive &&
1718
!candiateType.IsEnum &&
1819
!typeof(UnityEngine.Object).IsAssignableFrom(candiateType) &&

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/TypeSearch/TypeCandiateProvider/DefaultTypeCandiateProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public IEnumerable<Type> GetTypeCandidates (Type baseType)
2121
{
2222
return TypeCache.GetTypesDerivedFrom(baseType)
2323
.Append(baseType)
24-
.Where(intrinsicTypePolicy.IsAllowed);
24+
.Where(x => intrinsicTypePolicy.IsAllowed(x, false));
2525
}
2626
}
2727
}

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/TypeSearch/TypeCandiateProvider/Unity_2023_2_OrNewer_TypeCandiateProvider.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Reflection;
5+
using UnityEditor;
56

67
namespace MackySoft.SerializeReferenceExtensions.Editor
78
{
@@ -46,23 +47,30 @@ private IEnumerable<Type> GetTypesWithGeneric (Type baseType)
4647

4748
result = new List<Type>();
4849

49-
IEnumerable<Type> types = EnumerateAllTypesSafely();
50+
//
51+
var isGenericBaseType = baseType.IsGenericType;
52+
var genericTypeDefinition = isGenericBaseType ? baseType.GetGenericTypeDefinition() : baseType;
53+
var targetTypeArguments = isGenericBaseType ? baseType.GetGenericArguments() : Type.EmptyTypes;
54+
IEnumerable<Type> types = TypeCache.GetTypesDerivedFrom(genericTypeDefinition);
5055
foreach (Type type in types)
5156
{
52-
if (!intrinsicTypePolicy.IsAllowed(type))
57+
// If the type is Generic, create a MakeGenericType from the Arguments of the baseType.
58+
var targetType = type.IsGenericType ? type.MakeGenericType(targetTypeArguments) : type;
59+
60+
if (!intrinsicTypePolicy.IsAllowed(targetType, targetType != type))
5361
{
5462
continue;
5563
}
56-
if (!typeCompatibilityPolicy.IsCompatible(baseType, type))
64+
if (!typeCompatibilityPolicy.IsCompatible(baseType, targetType))
5765
{
5866
continue;
5967
}
6068

61-
result.Add(type);
69+
result.Add(targetType);
6270
}
6371

6472
// Include the base type itself if allowed
65-
if (intrinsicTypePolicy.IsAllowed(baseType) && typeCompatibilityPolicy.IsCompatible(baseType, baseType))
73+
if (intrinsicTypePolicy.IsAllowed(baseType, false) && typeCompatibilityPolicy.IsCompatible(baseType, baseType))
6674
{
6775
result.Add(baseType);
6876
}

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Tests/Editor/DefaultIntrinsicTypePolicyTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static IEnumerable<TestCaseData> Cases ()
2626
[TestCaseSource(nameof(Cases))]
2727
public void IsAllowed_MatchesExpected (Type type, bool expected)
2828
{
29-
bool actual = DefaultIntrinsicTypePolicy.Instance.IsAllowed(type);
29+
bool actual = DefaultIntrinsicTypePolicy.Instance.IsAllowed(type, false);
3030
Assert.That(actual, Is.EqualTo(expected), type.FullName);
3131
}
3232
}

0 commit comments

Comments
 (0)