File tree Expand file tree Collapse file tree 1 file changed +37
-6
lines changed
GameFrameX.Utility/Extensions Expand file tree Collapse file tree 1 file changed +37
-6
lines changed Original file line number Diff line number Diff line change @@ -7,17 +7,48 @@ public static class TypeExtensions
77{
88 /// <summary>
99 /// 判断类型是否实现了指定的接口。
10+ /// 此方法用于检查一个具体类型是否实现了目标接口。
1011 /// </summary>
11- /// <param name="self">要判断的类型。</param>
12- /// <param name="target">要判断的接口类型。</param>
13- /// <returns>如果类型实现了指定的接口且不是接口类型或抽象类型,则为 true;否则为 false。</returns>
14- public static bool IsImplWithInterface ( this Type self , Type target )
12+ /// <param name="self">要判断的类型。必须是非空的具体类型。</param>
13+ /// <param name="target">要判断的接口类型。必须是非空的接口类型。</param>
14+ /// <param name="directOnly">是否只检查直接实现的接口。
15+ /// 当设置为true时,只检查直接实现的接口;
16+ /// 当设置为false时,同时检查继承链上的所有接口。</param>
17+ /// <returns>
18+ /// 满足以下所有条件时返回true:
19+ /// 1. self和target参数都不为null
20+ /// 2. target是接口类型
21+ /// 3. self不是接口类型或抽象类型
22+ /// 4. self实现了target接口(根据directOnly参数决定检查范围)
23+ /// 否则返回false
24+ /// </returns>
25+ public static bool IsImplWithInterface ( this Type self , Type target , bool directOnly = false )
1526 {
16- if ( target == null )
27+ // 参数有效性检查
28+ if ( target == null || self == null )
1729 {
1830 return false ;
1931 }
2032
21- return ! self . IsInterface && ! self . IsAbstract && target . FullName != null && self . GetInterface ( target . FullName ) != null ;
33+ // 确保target是接口类型
34+ if ( ! target . IsInterface )
35+ {
36+ return false ;
37+ }
38+
39+ // 检查是否是接口类型或抽象类型
40+ if ( self . IsInterface || self . IsAbstract )
41+ {
42+ return false ;
43+ }
44+
45+ if ( directOnly )
46+ {
47+ // 只检查直接实现的接口
48+ return self . GetInterfaces ( ) . Any ( i => i == target ) ;
49+ }
50+
51+ // 检查所有实现的接口(包括继承的接口)
52+ return self . GetInterfaces ( ) . Any ( i => i == target || i . GetInterfaces ( ) . Contains ( target ) ) ;
2253 }
2354}
You can’t perform that action at this time.
0 commit comments