2 using System.Collections.Generic;
10 static class TypeHelper {
12 public static AssemblyResolver Resolver = new AssemblyResolver ();
14 internal static bool IsPublic (TypeReference typeref)
17 throw new ArgumentNullException ("typeref");
19 TypeDefinition td = typeref.Resolve ();
26 internal static bool IsDelegate (TypeReference typeref)
28 return IsDerivedFrom (typeref, "System.MulticastDelegate");
31 internal static bool IsDerivedFrom (TypeReference type, string derivedFrom)
34 foreach (var def in WalkHierarchy (type)) {
40 if (def.FullName == derivedFrom)
47 internal static IEnumerable<TypeDefinition> WalkHierarchy (TypeReference type)
49 for (var def = type.Resolve (); def != null; def = GetBaseType (def))
53 internal static IEnumerable<TypeReference> GetInterfaces (TypeReference type)
55 var ifaces = new Dictionary<string, TypeReference> ();
57 foreach (var def in WalkHierarchy (type))
58 foreach (TypeReference iface in def.Interfaces)
59 ifaces [iface.FullName] = iface;
64 internal static TypeDefinition GetBaseType (TypeDefinition child)
66 if (child.BaseType == null)
69 return child.BaseType.Resolve ();
72 internal static bool IsPublic (CustomAttribute att)
74 return IsPublic (att.AttributeType);
77 internal static string GetFullName (CustomAttribute att)
79 return att.AttributeType.FullName;
82 internal static TypeDefinition GetTypeDefinition (CustomAttribute att)
84 return att.AttributeType.Resolve ();
87 static bool IsOverride (MethodDefinition method)
89 return method.IsVirtual && !method.IsNewSlot;
92 public static MethodDefinition GetBaseMethodInTypeHierarchy (MethodDefinition method)
94 if (!IsOverride (method))
97 var @base = GetBaseType (method.DeclaringType);
98 while (@base != null) {
99 MethodDefinition base_method = TryMatchMethod (@base.Resolve (), method);
100 if (base_method != null)
101 return GetBaseMethodInTypeHierarchy (base_method) ?? base_method;
103 @base = GetBaseType (@base);
109 static MethodDefinition TryMatchMethod (TypeDefinition type, MethodDefinition method)
111 if (!type.HasMethods)
114 foreach (MethodDefinition candidate in type.Methods)
115 if (MethodMatch (candidate, method))
121 static bool MethodMatch (MethodDefinition candidate, MethodDefinition method)
123 if (!candidate.IsVirtual)
126 if (candidate.Name != method.Name)
129 if (!TypeMatch (candidate.ReturnType, method.ReturnType))
132 if (candidate.Parameters.Count != method.Parameters.Count)
135 for (int i = 0; i < candidate.Parameters.Count; i++)
136 if (!TypeMatch (candidate.Parameters [i].ParameterType, method.Parameters [i].ParameterType))
142 public static bool TypeMatch (IModifierType a, IModifierType b)
144 if (!TypeMatch (a.ModifierType, b.ModifierType))
147 return TypeMatch (a.ElementType, b.ElementType);
150 public static bool TypeMatch (TypeSpecification a, TypeSpecification b)
152 if (a is GenericInstanceType)
153 return TypeMatch ((GenericInstanceType) a, (GenericInstanceType) b);
155 if (a is IModifierType)
156 return TypeMatch ((IModifierType) a, (IModifierType) b);
158 return TypeMatch (a.ElementType, b.ElementType);
161 public static bool TypeMatch (GenericInstanceType a, GenericInstanceType b)
163 if (!TypeMatch (a.ElementType, b.ElementType))
166 if (a.GenericArguments.Count != b.GenericArguments.Count)
169 if (a.GenericArguments.Count == 0)
172 for (int i = 0; i < a.GenericArguments.Count; i++)
173 if (!TypeMatch (a.GenericArguments [i], b.GenericArguments [i]))
179 public static bool TypeMatch (TypeReference a, TypeReference b)
181 if (a is GenericParameter)
184 if (a is TypeSpecification || b is TypeSpecification) {
185 if (a.GetType () != b.GetType ())
188 return TypeMatch ((TypeSpecification) a, (TypeSpecification) b);
191 return a.FullName == b.FullName;