// dnlib: See LICENSE.txt for more info using System.Reflection; namespace dnlib.DotNet { /// /// Resolves assemblies /// public interface IAssemblyResolver { /// /// Finds and returns an /// /// The assembly to find /// The module that needs to resolve an assembly or null /// An instance owned by the assembly resolver or /// null if the assembly couldn't be found. AssemblyDef Resolve(IAssembly assembly, ModuleDef sourceModule); } public static partial class Extensions { /// /// Finds and returns an /// /// this /// The assembly to find /// The module that needs to resolve an assembly or null /// An instance owned by the assembly resolver or /// null if the assembly couldn't be found. public static AssemblyDef Resolve(this IAssemblyResolver self, AssemblyName assembly, ModuleDef sourceModule) { if (assembly is null) return null; return self.Resolve(new AssemblyNameInfo(assembly), sourceModule); } /// /// Finds and returns an /// /// this /// The assembly to find /// The module that needs to resolve an assembly or null /// An instance owned by the assembly resolver or /// null if the assembly couldn't be found. public static AssemblyDef Resolve(this IAssemblyResolver self, string asmFullName, ModuleDef sourceModule) { if (asmFullName is null) return null; return self.Resolve(new AssemblyNameInfo(asmFullName), sourceModule); } /// /// Finds and returns an /// /// this /// The assembly to find /// The module that needs to resolve an assembly or null /// An instance owned by the assembly resolver /// If the assembly couldn't be found. public static AssemblyDef ResolveThrow(this IAssemblyResolver self, IAssembly assembly, ModuleDef sourceModule) { if (assembly is null) return null; var asm = self.Resolve(assembly, sourceModule); if (asm is not null) return asm; throw new AssemblyResolveException($"Could not resolve assembly: {assembly}"); } /// /// Finds and returns an /// /// this /// The assembly to find /// The module that needs to resolve an assembly or null /// An instance owned by the assembly resolver /// If the assembly couldn't be found. 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}"); } /// /// Finds and returns an /// /// this /// The assembly to find /// The module that needs to resolve an assembly or null /// An instance owned by the assembly resolver /// If the assembly couldn't be found. public static AssemblyDef ResolveThrow(this IAssemblyResolver self, string asmFullName, ModuleDef sourceModule) { if (asmFullName is null) return null; var asm = self.Resolve(new AssemblyNameInfo(asmFullName), sourceModule); if (asm is not null) return asm; throw new AssemblyResolveException($"Could not resolve assembly: {asmFullName}"); } } }