2009-01-28 Jb Evain <jbevain@novell.com>
[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                         return td.IsPublic;
21                 }
22
23                 internal static bool IsDelegate (TypeReference typeref)
24                 {
25                         return IsDerivedFrom (typeref, "System.MulticastDelegate");
26                 }
27
28                 internal static bool IsDerivedFrom (TypeReference type, string derivedFrom)
29                 {
30                         foreach (var def in WalkHierarchy (type))
31                                 if (def.FullName == derivedFrom)
32                                         return true;
33
34                         return false;
35                 }
36
37                 internal static IEnumerable<TypeDefinition> WalkHierarchy (TypeReference type)
38                 {
39                         for (var def = type.Resolve (); def != null; def = GetBaseType (def))
40                                 yield return def;
41                 }
42
43                 internal static IEnumerable<TypeReference> GetInterfaces (TypeReference type)
44                 {
45                         var ifaces = new Dictionary<string, TypeReference> ();
46
47                         foreach (var def in WalkHierarchy (type))
48                                 foreach (TypeReference iface in def.Interfaces)
49                                         ifaces [iface.FullName] = iface;
50
51                         return ifaces.Values;
52                 }
53
54                 internal static TypeDefinition GetBaseType (TypeDefinition child)
55                 {
56                         if (child.BaseType == null)
57                                 return null;
58
59                         return child.BaseType.Resolve ();
60                 }
61
62                 internal static bool IsPublic (CustomAttribute att)
63                 {
64                         return IsPublic (att.Constructor.DeclaringType);
65                 }
66
67                 internal static string GetFullName (CustomAttribute att)
68                 {
69                         return att.Constructor.DeclaringType.FullName;
70                 }
71
72                 internal static TypeDefinition GetTypeDefinition (CustomAttribute att)
73                 {
74                         return att.Constructor.DeclaringType.Resolve ();
75                 }
76         }
77 }