/// <summary> /// Resolve a TypeDef or TypeRef from its name. If neither a TypeDef or TypeRef are found /// in the module, search its references (AssemblyRefs) and if a match is found, add a TypeRef /// for it to the module and return that. /// </summary> /// <param name="fullName">Name of TypeDef or TypeRef as found in the resource</param> /// <param name="isReflectionName">Whether or not the name is a reflection name</param> /// <returns>TypeDef or TypeRef, or null if none found</returns> public ITypeDefOrRef ResolveTypeDefOrRef(TypeName typeName) { String fullName = typeName.Name; // Return TypeDef if found TypeDef typeDef = _module.Find(fullName, false); if (typeDef != null) return typeDef; // Return existing TypeRef if found var typeRefs = _module.GetTypeRefs(); foreach(var typeRef in typeRefs) { if (typeRef.FullName.Equals(fullName)) return typeRef; } // Get the AssemblyRef from the type name and make our own TypeRef AssemblyRef asmRef = this.FindAssemblyRef(typeName); if(!typeName.IsNested) return new TypeRefUser(_module, typeName.Namespace, typeName.NameWithoutNamespace, asmRef); else { // Lazy... var parentName = typeName.ParentName.Split('.').Last(); TypeRef resolutionRef = new TypeRefUser(_module, typeName.Namespace, parentName, asmRef); return new TypeRefUser(_module, "", typeName.NestedName, resolutionRef); } }
/// <summary> /// Get the AssemblyRef of the module from the assembly full name, adding /// our own AssemblyRef if none found. /// </summary> /// <param name="fullName">TypeName containing the assembly's full name</param> /// <returns>AssemblyRef</returns> public AssemblyRef FindAssemblyRef(TypeName typeName) { return this.FindAssemblyRef(typeName.AssemblyFullName); }
ITypeDefOrRef ResolveType_NoLock(Int32 position) { this.Stream.Position = position; InlineOperand operand = new InlineOperand(this.Reader); if (operand.IsToken) { MDToken token = new MDToken(operand.Token); if (token.Table == Table.TypeDef) return this.Module.ResolveTypeDef(token.Rid); else if (token.Table == Table.TypeRef) return this.Module.ResolveTypeRef(token.Rid); else if (token.Table == Table.TypeSpec) return this.Module.ResolveTypeSpec(token.Rid); throw new Exception("Unable to resolve type: bad MDToken table"); } else { TypeData data = operand.Data as TypeData; // Resolve via name TypeName typeName = new TypeName(data.Name); NameResolver nameResolver = new NameResolver(this.Module); ITypeDefOrRef typeDefOrRef = nameResolver.ResolveTypeDefOrRef(typeName); if (typeDefOrRef == null) { throw new Exception(String.Format( "Unable to resolve ITypeDefOrRef from given name: {0}", typeName.FullName)); } // Apply generics, if any (resulting in a TypeSpec) if (data.GenericTypes.Length > 0) typeDefOrRef = ApplyGenerics(typeDefOrRef, data); if (typeDefOrRef == null) { throw new Exception(String.Format( "Unable to apply generic types: {0}", typeName.FullName )); } // Apply [], *, & typeDefOrRef = SigUtil.FromBaseSig(typeDefOrRef.ToTypeSig(), typeName.Modifiers) .ToTypeDefOrRef(); return typeDefOrRef; } }