| 1 |
// |
| 2 |
// MetadataResolver.cs |
| 3 |
// |
| 4 |
// Author: |
| 5 |
// Jb Evain ([email protected]) |
| 6 |
// |
| 7 |
// (C) 2008 Jb Evain (http://evain.net) |
| 8 |
// (C) 2008 Novell, Inc. (http://www.novell.com) |
| 9 |
// |
| 10 |
// Permission is hereby granted, free of charge, to any person obtaining |
| 11 |
// a copy of this software and associated documentation files (the |
| 12 |
// "Software"), to deal in the Software without restriction, including |
| 13 |
// without limitation the rights to use, copy, modify, merge, publish, |
| 14 |
// distribute, sublicense, and/or sell copies of the Software, and to |
| 15 |
// permit persons to whom the Software is furnished to do so, subject to |
| 16 |
// the following conditions: |
| 17 |
// |
| 18 |
// The above copyright notice and this permission notice shall be |
| 19 |
// included in all copies or substantial portions of the Software. |
| 20 |
// |
| 21 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 23 |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 24 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 25 |
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 26 |
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 27 |
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 28 |
// |
| 29 |
|
| 30 |
using System; |
| 31 |
using System.Collections; |
| 32 |
|
| 33 |
namespace Mono.Cecil { |
| 34 |
|
| 35 |
class MetadataResolver { |
| 36 |
|
| 37 |
AssemblyDefinition assembly; |
| 38 |
|
| 39 |
public IAssemblyResolver AssemblyResolver { |
| 40 |
get { return assembly.Resolver; } |
| 41 |
} |
| 42 |
|
| 43 |
public MetadataResolver (AssemblyDefinition assembly) |
| 44 |
{ |
| 45 |
this.assembly = assembly; |
| 46 |
} |
| 47 |
|
| 48 |
public TypeDefinition Resolve (TypeReference type) |
| 49 |
{ |
| 50 |
type = type.GetOriginalType (); |
| 51 |
|
| 52 |
if (type is TypeDefinition) |
| 53 |
return (TypeDefinition) type; |
| 54 |
|
| 55 |
AssemblyNameReference reference = type.Scope as AssemblyNameReference; |
| 56 |
if (reference != null) { |
| 57 |
AssemblyDefinition assembly = AssemblyResolver.Resolve (reference); |
| 58 |
if (assembly == null) |
| 59 |
return null; |
| 60 |
|
| 61 |
return assembly.MainModule.Types [type.FullName]; |
| 62 |
} |
| 63 |
|
| 64 |
ModuleDefinition module = type.Scope as ModuleDefinition; |
| 65 |
if (module != null) |
| 66 |
return module.Types [type.FullName]; |
| 67 |
|
| 68 |
ModuleReference mod_reference = type.Scope as ModuleReference; |
| 69 |
if (mod_reference != null) { |
| 70 |
foreach (ModuleDefinition netmodule in type.Module.Assembly.Modules) |
| 71 |
if (netmodule.Name == mod_reference.Name) |
| 72 |
return netmodule.Types [type.FullName]; |
| 73 |
} |
| 74 |
|
| 75 |
throw new NotImplementedException (); |
| 76 |
} |
| 77 |
|
| 78 |
public FieldDefinition Resolve (FieldReference field) |
| 79 |
{ |
| 80 |
TypeDefinition type = Resolve (field.DeclaringType); |
| 81 |
if (type == null) |
| 82 |
return null; |
| 83 |
|
| 84 |
return type.HasFields ? GetField (type.Fields, field) : null; |
| 85 |
} |
| 86 |
|
| 87 |
static FieldDefinition GetField (ICollection collection, FieldReference reference) |
| 88 |
{ |
| 89 |
foreach (FieldDefinition field in collection) { |
| 90 |
if (field.Name != reference.Name) |
| 91 |
continue; |
| 92 |
|
| 93 |
if (!AreSame (field.FieldType, reference.FieldType)) |
| 94 |
continue; |
| 95 |
|
| 96 |
return field; |
| 97 |
} |
| 98 |
|
| 99 |
return null; |
| 100 |
} |
| 101 |
|
| 102 |
public MethodDefinition Resolve (MethodReference method) |
| 103 |
{ |
| 104 |
TypeDefinition type = Resolve (method.DeclaringType); |
| 105 |
if (type == null) |
| 106 |
return null; |
| 107 |
|
| 108 |
method = method.GetOriginalMethod (); |
| 109 |
if (method.Name == MethodDefinition.Cctor || method.Name == MethodDefinition.Ctor) |
| 110 |
return type.HasConstructors ? GetMethod (type.Constructors, method) : null; |
| 111 |
else |
| 112 |
return type.HasMethods ? GetMethod (type, method) : null; |
| 113 |
} |
| 114 |
|
| 115 |
MethodDefinition GetMethod (TypeDefinition type, MethodReference reference) |
| 116 |
{ |
| 117 |
while (type != null) { |
| 118 |
MethodDefinition method = GetMethod (type.Methods, reference); |
| 119 |
if (method == null) { |
| 120 |
if (type.BaseType == null) |
| 121 |
return null; |
| 122 |
|
| 123 |
type = Resolve (type.BaseType); |
| 124 |
} else |
| 125 |
return method; |
| 126 |
} |
| 127 |
|
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
static MethodDefinition GetMethod (ICollection collection, MethodReference reference) |
| 132 |
{ |
| 133 |
foreach (MethodDefinition meth in collection) { |
| 134 |
if (meth.Name != reference.Name) |
| 135 |
continue; |
| 136 |
|
| 137 |
if (!AreSame (meth.ReturnType.ReturnType, reference.ReturnType.ReturnType)) |
| 138 |
continue; |
| 139 |
|
| 140 |
if (meth.HasParameters != reference.HasParameters) |
| 141 |
continue; |
| 142 |
|
| 143 |
if (!meth.HasParameters && !reference.HasParameters) |
| 144 |
return meth; //both have no parameters hence meth is the good one |
| 145 |
|
| 146 |
if (!AreSame (meth.Parameters, reference.Parameters)) |
| 147 |
continue; |
| 148 |
|
| 149 |
return meth; |
| 150 |
} |
| 151 |
|
| 152 |
return null; |
| 153 |
} |
| 154 |
|
| 155 |
static bool AreSame (ParameterDefinitionCollection a, ParameterDefinitionCollection b) |
| 156 |
{ |
| 157 |
if (a.Count != b.Count) |
| 158 |
return false; |
| 159 |
|
| 160 |
if (a.Count == 0) |
| 161 |
return true; |
| 162 |
|
| 163 |
for (int i = 0; i < a.Count; i++) |
| 164 |
if (!AreSame (a [i].ParameterType, b [i].ParameterType)) |
| 165 |
return false; |
| 166 |
|
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
static bool AreSame (ModType a, ModType b) |
| 171 |
{ |
| 172 |
if (!AreSame (a.ModifierType, b.ModifierType)) |
| 173 |
return false; |
| 174 |
|
| 175 |
return AreSame (a.ElementType, b.ElementType); |
| 176 |
} |
| 177 |
|
| 178 |
static bool AreSame (TypeSpecification a, TypeSpecification b) |
| 179 |
{ |
| 180 |
if (a is GenericInstanceType) |
| 181 |
return AreSame ((GenericInstanceType) a, (GenericInstanceType) b); |
| 182 |
|
| 183 |
if (a is ModType) |
| 184 |
return AreSame ((ModType) a, (ModType) b); |
| 185 |
|
| 186 |
return AreSame (a.ElementType, b.ElementType); |
| 187 |
} |
| 188 |
|
| 189 |
static bool AreSame (GenericInstanceType a, GenericInstanceType b) |
| 190 |
{ |
| 191 |
if (!AreSame (a.ElementType, b.ElementType)) |
| 192 |
return false; |
| 193 |
|
| 194 |
if (a.GenericArguments.Count != b.GenericArguments.Count) |
| 195 |
return false; |
| 196 |
|
| 197 |
if (a.GenericArguments.Count == 0) |
| 198 |
return true; |
| 199 |
|
| 200 |
for (int i = 0; i < a.GenericArguments.Count; i++) |
| 201 |
if (!AreSame (a.GenericArguments [i], b.GenericArguments [i])) |
| 202 |
return false; |
| 203 |
|
| 204 |
return true; |
| 205 |
} |
| 206 |
|
| 207 |
static bool AreSame (GenericParameter a, GenericParameter b) |
| 208 |
{ |
| 209 |
return a.Position == b.Position; |
| 210 |
} |
| 211 |
|
| 212 |
static bool AreSame (TypeReference a, TypeReference b) |
| 213 |
{ |
| 214 |
if (a is TypeSpecification || b is TypeSpecification) { |
| 215 |
if (a.GetType () != b.GetType ()) |
| 216 |
return false; |
| 217 |
|
| 218 |
return AreSame ((TypeSpecification) a, (TypeSpecification) b); |
| 219 |
} |
| 220 |
|
| 221 |
if (a is GenericParameter || b is GenericParameter) { |
| 222 |
if (a.GetType () != b.GetType ()) |
| 223 |
return false; |
| 224 |
|
| 225 |
return AreSame ((GenericParameter) a, (GenericParameter) b); |
| 226 |
} |
| 227 |
|
| 228 |
return a.FullName == b.FullName; |
| 229 |
} |
| 230 |
} |
| 231 |
} |