Port corcompare to cecil-light
[mono.git] / mcs / tools / corcompare / Util.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Mono.Cecil;
5
6 using GuiCompare;
7
8 namespace CorCompare {
9
10         static class TypeHelper {
11
12                 public static AssemblyResolver Resolver = new AssemblyResolver ();
13
14                 internal static bool IsPublic (TypeReference typeref)
15                 {
16                         if (typeref == null)
17                                 throw new ArgumentNullException ("typeref");
18
19                         TypeDefinition td = typeref.Resolve ();
20                         if (td == null)
21                                 return false;
22
23                         return td.IsPublic;
24                 }
25
26                 internal static bool IsDelegate (TypeReference typeref)
27                 {
28                         return IsDerivedFrom (typeref, "System.MulticastDelegate");
29                 }
30
31                 internal static bool IsDerivedFrom (TypeReference type, string derivedFrom)
32                 {
33                         foreach (var def in WalkHierarchy (type))
34                                 if (def.FullName == derivedFrom)
35                                         return true;
36
37                         return false;
38                 }
39
40                 internal static IEnumerable<TypeDefinition> WalkHierarchy (TypeReference type)
41                 {
42                         for (var def = type.Resolve (); def != null; def = GetBaseType (def))
43                                 yield return def;
44                 }
45
46                 internal static IEnumerable<TypeReference> GetInterfaces (TypeReference type)
47                 {
48                         var ifaces = new Dictionary<string, TypeReference> ();
49
50                         foreach (var def in WalkHierarchy (type))
51                                 foreach (TypeReference iface in def.Interfaces)
52                                         ifaces [iface.FullName] = iface;
53
54                         return ifaces.Values;
55                 }
56
57                 internal static TypeDefinition GetBaseType (TypeDefinition child)
58                 {
59                         if (child.BaseType == null)
60                                 return null;
61
62                         return child.BaseType.Resolve ();
63                 }
64
65                 internal static bool IsPublic (CustomAttribute att)
66                 {
67                         return IsPublic (att.AttributeType);
68                 }
69
70                 internal static string GetFullName (CustomAttribute att)
71                 {
72                         return att.AttributeType.FullName;
73                 }
74
75                 internal static TypeDefinition GetTypeDefinition (CustomAttribute att)
76                 {
77                         return att.AttributeType.Resolve ();
78                 }
79         }
80 }