Skip to content

Commit b9e324d

Browse files
committed
feat(扩展): 添加类型接口检查方法
新增HasInterface方法用于检查类型是否实现指定接口,支持递归检查继承链和接口实现
1 parent ff71722 commit b9e324d

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

GameFrameX.Foundation.Extensions/TypeExtensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ namespace GameFrameX.Foundation.Extensions;
1313
/// </summary>
1414
public static partial class TypeExtensions
1515
{
16+
/// <summary>
17+
/// 判断类型是否实现了指定的接口
18+
/// </summary>
19+
/// <param name="targetType">要检查的类型,不能为null</param>
20+
/// <param name="interfaceType">目标接口类型,不能为null</param>
21+
/// <returns>
22+
/// 如果类型实现了指定的接口,则返回true;
23+
/// 否则返回false
24+
/// </returns>
25+
/// <remarks>
26+
/// 此方法会检查类型是否直接或间接地实现了指定的接口。
27+
/// 它会递归检查类型的继承链和接口实现。
28+
/// </remarks>
29+
/// <exception cref="ArgumentNullException">当<paramref name="targetType"/>或<paramref name="interfaceType"/>为null时抛出</exception>
30+
public static bool HasInterface(Type targetType, Type interfaceType)
31+
{
32+
if (targetType == null || interfaceType == null || !interfaceType.IsInterface)
33+
{
34+
return false;
35+
}
36+
37+
return interfaceType.IsAssignableFrom(targetType);
38+
}
39+
1640
/// <summary>
1741
/// 判断类型是否实现某个泛型接口或继承自某个泛型类
1842
/// </summary>

0 commit comments

Comments
 (0)