/// <summary> /// Constructor /// </summary> /// <param name="module">The owner module</param> public CorLibTypes(ModuleDef module) : this(module, null) { }
/// <summary> /// Finds and returns an <see cref="AssemblyDef"/> /// </summary> /// <param name="self">this</param> /// <param name="assembly">The assembly to find</param> /// <param name="sourceModule">The module that needs to resolve an assembly or <c>null</c></param> /// <returns>An <see cref="AssemblyDef"/> instance owned by the assembly resolver or /// <c>null</c> if the assembly couldn't be found.</returns> public static AssemblyDef Resolve(this IAssemblyResolver self, AssemblyName assembly, ModuleDef sourceModule) { if (assembly == null) { return(null); } return(self.Resolve(new AssemblyNameInfo(assembly), sourceModule)); }
/// <summary> /// Constructor /// </summary> /// <param name="ownerModule">Module that will own the returned <see cref="IType"/> or <c>null</c></param> /// <param name="typeFullName">Full name of type</param> /// <param name="typeNameParserHelper">Helper class</param> public ReflectionTypeNameParser(ModuleDef ownerModule, string typeFullName, IAssemblyRefFinder typeNameParserHelper) : base(ownerModule, typeFullName, typeNameParserHelper) { }
/// <summary> /// Finds and returns an <see cref="AssemblyDef"/> /// </summary> /// <param name="self">this</param> /// <param name="asmFullName">The assembly to find</param> /// <param name="sourceModule">The module that needs to resolve an assembly or <c>null</c></param> /// <returns>An <see cref="AssemblyDef"/> instance owned by the assembly resolver or /// <c>null</c> if the assembly couldn't be found.</returns> public static AssemblyDef Resolve(this IAssemblyResolver self, string asmFullName, ModuleDef sourceModule) { if (asmFullName == null) { return(null); } return(self.Resolve(new AssemblyNameInfo(asmFullName), sourceModule)); }
/// <summary> /// Add a module's assembly to the assembly cache /// </summary> /// <param name="self">this</param> /// <param name="module">The module whose assembly should be cached</param> /// <returns><c>true</c> if <paramref name="module"/>'s assembly is cached, <c>false</c> /// if it's not cached because some other assembly with the exact same full name has /// already been cached or if <paramref name="module"/> or its assembly is <c>null</c>.</returns> public static bool AddToCache(this IAssemblyResolver self, ModuleDef module) { return(module != null && self.AddToCache(module.Assembly)); }
/// <summary> /// Gets all module search paths. This is usually empty unless its assembly has /// a <c>.config</c> file specifying any additional private search paths in a /// <probing/> element. /// </summary> /// <param name="module">The module or <c>null</c> if unknown</param> /// <returns>A list of search paths</returns> protected virtual IEnumerable <string> GetModuleSearchPaths(ModuleDef module) { return(GetModulePrivateSearchPaths(module)); }
public static void LoadAll(ModuleDef module, ICancellationToken cancellationToken) { new ModuleLoader(module, cancellationToken).Load(); }
/// <inheritdoc/> public TypeDef Resolve(TypeRef typeRef, ModuleDef sourceModule) { if (typeRef == null) { return(null); } if (ProjectWinMDRefs) { typeRef = WinMDHelpers.ToCLR(typeRef.Module ?? sourceModule, typeRef) ?? typeRef; } var nonNestedTypeRef = TypeRef.GetNonNestedTypeRef(typeRef); if (nonNestedTypeRef == null) { return(null); } var nonNestedResolutionScope = nonNestedTypeRef.ResolutionScope; var nonNestedModule = nonNestedTypeRef.Module; var asmRef = nonNestedResolutionScope as AssemblyRef; if (asmRef != null) { var asm = assemblyResolver.Resolve(asmRef, sourceModule ?? nonNestedModule); return(asm == null ? null : asm.Find(typeRef) ?? ResolveExportedType(asm.Modules, typeRef, sourceModule)); } var moduleDef = nonNestedResolutionScope as ModuleDef; if (moduleDef != null) { return(moduleDef.Find(typeRef) ?? ResolveExportedType(new ModuleDef[] { moduleDef }, typeRef, sourceModule)); } var moduleRef = nonNestedResolutionScope as ModuleRef; if (moduleRef != null) { if (nonNestedModule == null) { return(null); } if (new SigComparer().Equals(moduleRef, nonNestedModule)) { return(nonNestedModule.Find(typeRef) ?? ResolveExportedType(new ModuleDef[] { nonNestedModule }, typeRef, sourceModule)); } var nonNestedAssembly = nonNestedModule.Assembly; if (nonNestedAssembly == null) { return(null); } var resolvedModule = nonNestedAssembly.FindModule(moduleRef.Name); return(resolvedModule == null ? null : resolvedModule.Find(typeRef) ?? ResolveExportedType(new ModuleDef[] { resolvedModule }, typeRef, sourceModule)); } return(null); }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="module">Owner module</param> /// <param name="stream">A stream positioned at the the first byte of the CA blob</param> /// <param name="ctor">Custom attribute constructor</param> /// <returns>A new <see cref="CustomAttribute"/> instance or <c>null</c> if one of the /// args is <c>null</c> or if we failed to parse the CA blob</returns> public static CustomAttribute Read(ModuleDef module, IBinaryReader stream, ICustomAttributeType ctor) { return(Read(module, stream, ctor, new GenericParamContext())); }
/// <summary> /// Constructor /// </summary> /// <param name="module">Owner module</param> /// <param name="name">Type name</param> public TypeRefUser(ModuleDef module, UTF8String name) : this(module, UTF8String.Empty, name) { }
/// <summary> /// Constructor /// </summary> /// <param name="module">Owner module</param> /// <param name="namespace">Type namespace</param> /// <param name="name">Type name</param> public TypeRefUser(ModuleDef module, UTF8String @namespace, UTF8String name) : this(module, @namespace, name, null) { }
/// <summary> /// Finds and returns an <see cref="AssemblyDef"/> /// </summary> /// <param name="self">this</param> /// <param name="assembly">The assembly to find</param> /// <param name="sourceModule">The module that needs to resolve an assembly or <c>null</c></param> /// <returns>An <see cref="AssemblyDef"/> instance owned by the assembly resolver</returns> /// <exception cref="AssemblyResolveException">If the assembly couldn't be found.</exception> public static AssemblyDef ResolveThrow(this IAssemblyResolver self, AssemblyName assembly, ModuleDef sourceModule) { if (assembly is null) { return(null); } var asm = self.Resolve(new AssemblyNameInfo(assembly), sourceModule); if (asm is not null) { return(asm); } throw new AssemblyResolveException($"Could not resolve assembly: {assembly}"); }
/// <summary> /// Tries to redirect a .NET Framework assembly from an older version to the correct version /// loaded at runtime. /// </summary> /// <param name="assembly">Assembly reference</param> /// <param name="sourceModule">Module using the assembly reference</param> /// <param name="redirectedAssembly">Updated with the redirected assembly if successful</param> /// <returns></returns> public static bool TryApplyFrameworkRedirect(IAssembly assembly, ModuleDef sourceModule, out IAssembly redirectedAssembly) => TryApplyFrameworkRedirectCore(assembly, sourceModule, out redirectedAssembly);
/// <summary> /// Constructor /// </summary> /// <param name="module">The owner module</param> /// <param name="corLibAssemblyRef">Corlib assembly reference or <c>null</c> if a default /// assembly reference should be created</param> public CorLibTypes(ModuleDef module, AssemblyRef corLibAssemblyRef) { this.module = module; this.corLibAssemblyRef = corLibAssemblyRef ?? CreateCorLibAssemblyRef(); Initialize(); }
IEnumerable <string> FindAssembliesGacExactly(GacInfo gacInfo, IAssembly assembly, ModuleDef sourceModule) { var pkt = PublicKeyBase.ToPublicKeyToken(assembly.PublicKeyOrToken); if (gacInfo != null && pkt != null) { string pktString = pkt.ToString(); string verString = Utils.CreateVersionWithNoUndefinedValues(assembly.Version).ToString(); var asmSimpleName = UTF8String.ToSystemStringOrEmpty(assembly.Name); foreach (var subDir in gacInfo.subDirs) { var baseDir = Path.Combine(gacInfo.path, subDir); baseDir = Path.Combine(baseDir, asmSimpleName); baseDir = Path.Combine(baseDir, string.Format("{0}{1}__{2}", gacInfo.prefix, verString, pktString)); var pathName = Path.Combine(baseDir, asmSimpleName + ".dll"); if (File.Exists(pathName)) { yield return(pathName); } } } }
/// <summary> /// Parses a Reflection type name and creates a <see cref="TypeSig"/> /// </summary> /// <param name="ownerModule">Module that will own the returned <see cref="TypeSig"/> or <c>null</c></param> /// <param name="typeFullName">Full name of type</param> /// <param name="typeNameParserHelper">Helper class</param> /// <returns>A new <see cref="TypeSig"/> instance</returns> /// <exception cref="TypeNameParserException">If parsing failed</exception> public static TypeSig ParseAsTypeSigReflectionThrow(ModuleDef ownerModule, string typeFullName, IAssemblyRefFinder typeNameParserHelper) { return(ParseAsTypeSigReflectionThrow(ownerModule, typeFullName, typeNameParserHelper, new GenericParamContext())); }
IEnumerable <string> FindAssembliesGacAny(GacInfo gacInfo, IAssembly assembly, ModuleDef sourceModule) { if (gacInfo != null) { var asmSimpleName = UTF8String.ToSystemStringOrEmpty(assembly.Name); foreach (var subDir in gacInfo.subDirs) { var baseDir = Path.Combine(gacInfo.path, subDir); baseDir = Path.Combine(baseDir, asmSimpleName); foreach (var dir in GetDirs(baseDir)) { var pathName = Path.Combine(dir, asmSimpleName + ".dll"); if (File.Exists(pathName)) { yield return(pathName); } } } } }
/// <summary> /// Parses a Reflection type name and creates a <see cref="TypeSig"/> /// </summary> /// <param name="ownerModule">Module that will own the returned <see cref="TypeSig"/> or <c>null</c></param> /// <param name="typeFullName">Full name of type</param> /// <param name="typeNameParserHelper">Helper class</param> /// <param name="gpContext">Generic parameter context</param> /// <returns>A new <see cref="TypeSig"/> instance</returns> /// <exception cref="TypeNameParserException">If parsing failed</exception> public static TypeSig ParseAsTypeSigReflectionThrow(ModuleDef ownerModule, string typeFullName, IAssemblyRefFinder typeNameParserHelper, GenericParamContext gpContext) { using (var parser = new ReflectionTypeNameParser(ownerModule, typeFullName, typeNameParserHelper, gpContext)) return(parser.ParseAsTypeSig()); }
/// <summary> /// Gets all private assembly search paths as found in the module's <c>.config</c> file. /// </summary> /// <param name="module">The module or <c>null</c> if unknown</param> /// <returns>A list of search paths</returns> protected IEnumerable <string> GetModulePrivateSearchPaths(ModuleDef module) { if (module == null) { return(new string[0]); } var asm = module.Assembly; if (asm == null) { return(new string[0]); } module = asm.ManifestModule; if (module == null) { return(new string[0]); // Should never happen } string baseDir = null; try { var imageName = module.Location; if (imageName != string.Empty) { baseDir = Directory.GetParent(imageName).FullName; var configName = imageName + ".config"; if (File.Exists(configName)) { return(GetPrivatePaths(baseDir, configName)); } } } catch { } if (baseDir != null) { return new List <string> { baseDir } } ; return(new string[0]); } IEnumerable <string> GetPrivatePaths(string baseDir, string configFileName) { var searchPaths = new List <string>(); try { var dirName = Path.GetDirectoryName(Path.GetFullPath(configFileName)); searchPaths.Add(dirName); using (var xmlStream = new FileStream(configFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { var doc = new XmlDocument(); doc.Load(XmlReader.Create(xmlStream)); foreach (var tmp in doc.GetElementsByTagName("probing")) { var probingElem = tmp as XmlElement; if (probingElem == null) { continue; } var privatePath = probingElem.GetAttribute("privatePath"); if (string.IsNullOrEmpty(privatePath)) { continue; } foreach (var tmp2 in privatePath.Split(';')) { var path = tmp2.Trim(); if (path == "") { continue; } var newPath = Path.GetFullPath(Path.Combine(dirName, path.Replace('\\', Path.DirectorySeparatorChar))); if (Directory.Exists(newPath) && newPath.StartsWith(baseDir + Path.DirectorySeparatorChar)) { searchPaths.Add(newPath); } } } } } catch (ArgumentException) { } catch (IOException) { } catch (XmlException) { } return(searchPaths); }
/// <summary> /// Constructor /// </summary> /// <param name="ownerModule">Module that will own the returned <see cref="IType"/> or <c>null</c></param> /// <param name="typeFullName">Full name of type</param> /// <param name="typeNameParserHelper">Helper class</param> protected TypeNameParser(ModuleDef ownerModule, string typeFullName, IAssemblyRefFinder typeNameParserHelper) : this(ownerModule, typeFullName, typeNameParserHelper, new GenericParamContext()) { }
/// <summary> /// Constructor /// </summary> /// <param name="module">The module to search first</param> public CAAssemblyRefFinder(ModuleDef module) { this.module = module; }
/// <summary> /// Constructor /// </summary> /// <param name="ownerModule">Module that will own the returned <see cref="IType"/> or <c>null</c></param> /// <param name="typeFullName">Full name of type</param> /// <param name="typeNameParserHelper">Helper class</param> /// <param name="gpContext">Generic parameter context</param> public ReflectionTypeNameParser(ModuleDef ownerModule, string typeFullName, IAssemblyRefFinder typeNameParserHelper, GenericParamContext gpContext) : base(ownerModule, typeFullName, typeNameParserHelper, gpContext) { }
/// <summary> /// Finds and returns an <see cref="AssemblyDef"/> /// </summary> /// <param name="self">this</param> /// <param name="asmFullName">The assembly to find</param> /// <param name="sourceModule">The module that needs to resolve an assembly or <c>null</c></param> /// <returns>An <see cref="AssemblyDef"/> instance owned by the assembly resolver</returns> /// <exception cref="AssemblyResolveException">If the assembly couldn't be found.</exception> public static AssemblyDef ResolveThrow(this IAssemblyResolver self, string asmFullName, ModuleDef sourceModule) { if (asmFullName == null) { return(null); } var asm = self.Resolve(new AssemblyNameInfo(asmFullName), sourceModule); if (asm != null) { return(asm); } throw new AssemblyResolveException(string.Format("Could not resolve assembly: {0}", asmFullName)); }
/// <summary> /// Parses a Reflection type name and creates a <see cref="ITypeDefOrRef"/> /// </summary> /// <param name="ownerModule">Module that will own the returned <see cref="ITypeDefOrRef"/> or <c>null</c></param> /// <param name="typeFullName">Full name of type</param> /// <param name="typeNameParserHelper">Helper class</param> /// <returns>A new <see cref="ITypeDefOrRef"/> instance or <c>null</c> if parsing failed</returns> public static ITypeDefOrRef ParseReflection(ModuleDef ownerModule, string typeFullName, IAssemblyRefFinder typeNameParserHelper) { return(ParseReflection(ownerModule, typeFullName, typeNameParserHelper, new GenericParamContext())); }
/// <summary> /// Removes a module's assembly from the cache /// </summary> /// <param name="self">this</param> /// <param name="module">The module</param> /// <returns><c>true</c> if its assembly was removed, <c>false</c> if it wasn't removed /// since it wasn't in the cache, it has no assembly, or <paramref name="module"/> was /// <c>null</c></returns> public static bool Remove(this IAssemblyResolver self, ModuleDef module) { return(module != null && self.Remove(module.Assembly)); }
/// <inheritdoc/> public AssemblyDef Resolve(IAssembly assembly, ModuleDef sourceModule) { if (assembly == null) { return(null); } if (EnableFrameworkRedirect && !FindExactMatch) { FrameworkRedirect.ApplyFrameworkRedirect(ref assembly, sourceModule); } #if THREAD_SAFE theLock.EnterWriteLock(); try { #endif AssemblyDef resolvedAssembly = Resolve2(assembly, sourceModule); if (resolvedAssembly == null) { string asmName = UTF8String.ToSystemStringOrEmpty(assembly.Name); string asmNameTrimmed = asmName.Trim(); if (asmName != asmNameTrimmed) { assembly = new AssemblyNameInfo { Name = asmNameTrimmed, Version = assembly.Version, PublicKeyOrToken = assembly.PublicKeyOrToken, Culture = assembly.Culture, }; resolvedAssembly = Resolve2(assembly, sourceModule); } } if (resolvedAssembly == null) { // Make sure we don't search for this assembly again. This speeds up callers who // keep asking for this assembly when trying to resolve many different TypeRefs cachedAssemblies[GetAssemblyNameKey(assembly)] = null; return(null); } var key1 = GetAssemblyNameKey(resolvedAssembly); var key2 = GetAssemblyNameKey(assembly); AssemblyDef asm1, asm2; cachedAssemblies.TryGetValue(key1, out asm1); cachedAssemblies.TryGetValue(key2, out asm2); if (asm1 != resolvedAssembly && asm2 != resolvedAssembly) { // This assembly was just resolved if (enableTypeDefCache) { foreach (var module in resolvedAssembly.Modules.GetSafeEnumerable()) { if (module != null) { module.EnableTypeDefFindCache = true; } } } } bool inserted = false; if (!cachedAssemblies.ContainsKey(key1)) { cachedAssemblies.Add(key1, resolvedAssembly); inserted = true; } if (!cachedAssemblies.ContainsKey(key2)) { cachedAssemblies.Add(key2, resolvedAssembly); inserted = true; } if (inserted || asm1 == resolvedAssembly || asm2 == resolvedAssembly) { return(resolvedAssembly); } // Dupe assembly. Don't insert it. var dupeModule = resolvedAssembly.ManifestModule; if (dupeModule != null) { dupeModule.Dispose(); } return(asm1 ?? asm2); #if THREAD_SAFE } finally { theLock.ExitWriteLock(); } #endif }
/// <summary> /// Constructor /// </summary> /// <param name="ownerModule">Module that will own the returned <see cref="IType"/> or <c>null</c></param> /// <param name="typeFullName">Full name of type</param> /// <param name="typeNameParserHelper">Helper class</param> protected TypeNameParser(ModuleDef ownerModule, string typeFullName, IAssemblyRefFinder typeNameParserHelper) { this.ownerModule = ownerModule; this.reader = new StringReader(typeFullName ?? string.Empty); this.typeNameParserHelper = typeNameParserHelper; }
/// <summary> /// Called after <see cref="FindAssemblies"/> (if it fails) /// </summary> /// <param name="assembly">Assembly to find</param> /// <param name="sourceModule">The module that needs to resolve an assembly or <c>null</c></param> /// <param name="matchExactly">We're trying to find an exact match</param> /// <returns><c>null</c> or an enumerable of full paths to try</returns> protected virtual IEnumerable <string> PostFindAssemblies(IAssembly assembly, ModuleDef sourceModule, bool matchExactly) { foreach (var path in FindAssemblies2(assembly, postSearchPaths)) { yield return(path); } }
/// <summary> /// Parses a Reflection type name and creates a <see cref="ITypeDefOrRef"/> /// </summary> /// <param name="ownerModule">Module that will own the returned <see cref="ITypeDefOrRef"/> or <c>null</c></param> /// <param name="typeFullName">Full name of type</param> /// <param name="typeNameParserHelper">Helper class</param> /// <returns>A new <see cref="ITypeDefOrRef"/> instance</returns> /// <exception cref="TypeNameParserException">If parsing failed</exception> public static ITypeDefOrRef ParseReflectionThrow(ModuleDef ownerModule, string typeFullName, IAssemblyRefFinder typeNameParserHelper) { using (var parser = new ReflectionTypeNameParser(ownerModule, typeFullName, typeNameParserHelper)) return(parser.Parse()); }
/// <summary> /// Reads a custom attribute /// </summary> /// <param name="module">Owner module</param> /// <param name="caBlob">CA blob</param> /// <param name="ctor">Custom attribute constructor</param> /// <param name="gpContext">Generic parameter context</param> /// <returns>A new <see cref="CustomAttribute" /> instance</returns> public static CustomAttribute Read(ModuleDef module, byte[] caBlob, ICustomAttributeType ctor, GenericParamContext gpContext) { return(Read(module, MemoryImageStream.Create(caBlob), ctor, gpContext)); }