2010-05-25 Jb Evain <jbevain@novell.com>
[mcs.git] / tools / corcompare / Util.cs
blob9d3b3eb43b689b37cafd354282f9b43b76edb1e0
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Mono.Cecil;
6 using GuiCompare;
8 namespace CorCompare {
10 static class TypeHelper {
12 public static AssemblyResolver Resolver = new AssemblyResolver ();
14 internal static bool IsPublic (TypeReference typeref)
16 if (typeref == null)
17 throw new ArgumentNullException ("typeref");
19 TypeDefinition td = typeref.Resolve ();
20 if (td == null)
21 return false;
23 return td.IsPublic;
26 internal static bool IsDelegate (TypeReference typeref)
28 return IsDerivedFrom (typeref, "System.MulticastDelegate");
31 internal static bool IsDerivedFrom (TypeReference type, string derivedFrom)
33 foreach (var def in WalkHierarchy (type))
34 if (def.FullName == derivedFrom)
35 return true;
37 return false;
40 internal static IEnumerable<TypeDefinition> WalkHierarchy (TypeReference type)
42 for (var def = type.Resolve (); def != null; def = GetBaseType (def))
43 yield return def;
46 internal static IEnumerable<TypeReference> GetInterfaces (TypeReference type)
48 var ifaces = new Dictionary<string, TypeReference> ();
50 foreach (var def in WalkHierarchy (type))
51 foreach (TypeReference iface in def.Interfaces)
52 ifaces [iface.FullName] = iface;
54 return ifaces.Values;
57 internal static TypeDefinition GetBaseType (TypeDefinition child)
59 if (child.BaseType == null)
60 return null;
62 return child.BaseType.Resolve ();
65 internal static bool IsPublic (CustomAttribute att)
67 return IsPublic (att.Constructor.DeclaringType);
70 internal static string GetFullName (CustomAttribute att)
72 return att.Constructor.DeclaringType.FullName;
75 internal static TypeDefinition GetTypeDefinition (CustomAttribute att)
77 return att.Constructor.DeclaringType.Resolve ();