@@ -7,13 +7,13 @@ namespace Mono.Cecil
77{
88 static internal class TypeDefinitionExtensions
99 {
10- /// <summary>
11- /// Tests whether one class inherits from another.
12- /// </summary>
13- /// <param name="child">The class that is inheriting from the parent.</param>
14- /// <param name="parent">The parent that is inherited.</param>
15- /// <returns>An indication of whether the child inherits from the parent.</returns>
16- public static bool IsSubclassOf ( this TypeDefinition child , TypeDefinition parent )
10+ public static bool IsSubclassOf ( this TypeReference child , TypeReference parent )
11+ {
12+ var typeDef = child . Resolve ( ) ;
13+ return typeDef . IsSubclassOf ( parent ) ;
14+ }
15+
16+ public static bool IsSubclassOf ( this TypeDefinition child , TypeReference parent )
1717 {
1818 if ( parent != null )
1919 {
@@ -22,36 +22,8 @@ public static bool IsSubclassOf(this TypeDefinition child, TypeDefinition parent
2222 }
2323
2424 return false ;
25- }
26-
27- /// <summary>
28- /// Tests whether two type definitions are from the same assembly.
29- /// The comparison is based on the full assembly names.
30- /// </summary>
31- /// <param name="a"></param>
32- /// <param name="b"></param>
33- /// <returns>An indication of whether the both types are from the same assembly.</returns>
34- public static bool IsFromSameAssemblyAs ( this TypeDefinition a , TypeDefinition b )
35- {
36- return a . Module . Assembly . ToString ( ) == b . Module . Assembly . ToString ( ) ;
37- }
25+ }
3826
39- /// <summary>
40- /// Tests whether the provided types are the same type.
41- /// </summary>
42- /// <param name="a"></param>
43- /// <param name="b"></param>
44- /// <returns>An indication of whether the types are the same.</returns>
45- public static bool IsSameTypeAs ( this TypeDefinition a , TypeDefinition b )
46- {
47- return a . IsFromSameAssemblyAs ( b ) && a . MetadataToken == b . MetadataToken ;
48- }
49-
50- /// <summary>
51- /// Enumerate the base classes throughout the chain of inheritence.
52- /// </summary>
53- /// <param name="classType">The class to enumerate.</param>
54- /// <returns>The enumeration of base classes.</returns>
5527 private static IEnumerable < TypeDefinition > EnumerateBaseClasses ( this TypeDefinition classType )
5628 {
5729 for ( var typeDefinition = classType ; typeDefinition != null ; typeDefinition = typeDefinition . BaseType ? . Resolve ( ) )
@@ -60,27 +32,37 @@ private static IEnumerable<TypeDefinition> EnumerateBaseClasses(this TypeDefinit
6032 }
6133 }
6234
35+ public static bool IsAlmostEqualTo ( this TypeReference child , TypeDefinition parent )
36+ {
37+ if ( child is GenericInstanceType genericInstanceTypeB )
38+ {
39+ if ( parent . IsSameTypeAs ( genericInstanceTypeB . ElementType ) )
40+ {
41+ return true ;
42+ }
43+ }
44+
45+ if ( parent . IsSameTypeAs ( child ) )
46+ {
47+ return true ;
48+ }
49+
50+ return false ;
51+ }
52+
53+
54+
6355 /// <summary>
6456 /// Convert the definition to a <see cref="Type"/> object instance.
6557 /// </summary>
6658 /// <param name="typeDefinition">The type definition to convert.</param>
6759 /// <returns>The equivalent <see cref="Type"/> object instance.</returns>
6860 public static Type ToType ( this TypeDefinition typeDefinition )
6961 {
70- var fullName = RuntimeNameToReflectionName ( typeDefinition . FullName ) ;
62+ var fullName = typeDefinition . FullName . RuntimeNameToReflectionName ( ) ;
7163 return Type . GetType ( string . Concat ( fullName , ", " , typeDefinition . Module . Assembly . FullName ) , true ) ;
7264 }
73- public static string RuntimeNameToReflectionName ( this string cliName )
74- {
75- // Nested types have a forward slash that should be replaced with "+"
76- // C++ template instantiations contain comma separator for template arguments,
77- // getting address operators and pointer type designations which should be prefixed by backslash
78- var fullName = cliName . Replace ( "/" , "+" )
79- . Replace ( "," , "\\ ," )
80- . Replace ( "&" , "\\ &" )
81- . Replace ( "*" , "\\ *" ) ;
82- return fullName ;
83- }
65+
8466
8567
8668
@@ -150,9 +132,9 @@ public static string GetNameWithoutGenericPart(this TypeDefinition typeDefinitio
150132 }
151133 return typeDefinition . Name . RemoveGenericPart ( ) ;
152134 }
153-
154135
155136
137+
156138
157139
158140 public static bool IsDelegate ( this TypeDefinition typeDefinition )
0 commit comments