// dnlib: See LICENSE.txt for more info namespace dnlib.DotNet { /// /// Resolves types, methods, fields /// public interface IResolver : ITypeResolver, IMemberRefResolver { } /// /// Resolves types /// public interface ITypeResolver { /// /// Resolves a type /// /// The type /// The module that needs to resolve the type or null /// A instance or null if it couldn't be resolved TypeDef Resolve(TypeRef typeRef, ModuleDef sourceModule); } /// /// Resolves fields and methods /// public interface IMemberRefResolver { /// /// Resolves a method or a field /// /// A method/field reference /// A or a instance or null /// if it couldn't be resolved. IMemberForwarded Resolve(MemberRef memberRef); } public static partial class Extensions { /// /// Resolves a type /// /// this /// The type /// A instance or null if it couldn't be resolved public static TypeDef Resolve(this ITypeResolver self, TypeRef typeRef) => self.Resolve(typeRef, null); /// /// Resolves a type /// /// this /// The type /// A instance /// If the type couldn't be resolved public static TypeDef ResolveThrow(this ITypeResolver self, TypeRef typeRef) => self.ResolveThrow(typeRef, null); /// /// Resolves a type /// /// this /// The type /// The module that needs to resolve the type or null /// A instance /// If the type couldn't be resolved public static TypeDef ResolveThrow(this ITypeResolver self, TypeRef typeRef, ModuleDef sourceModule) { var type = self.Resolve(typeRef, sourceModule); if (type is not null) return type; throw new TypeResolveException($"Could not resolve type: {typeRef} ({typeRef?.DefinitionAssembly})"); } /// /// Resolves a method or a field /// /// this /// A method/field reference /// A or a instance /// If the method/field couldn't be resolved public static IMemberForwarded ResolveThrow(this IMemberRefResolver self, MemberRef memberRef) { var memberDef = self.Resolve(memberRef); if (memberDef is not null) return memberDef; throw new MemberRefResolveException($"Could not resolve method/field: {memberRef} ({memberRef?.GetDefinitionAssembly()})"); } /// /// Resolves a field /// /// this /// A field reference /// A instance or null if it couldn't be resolved. public static FieldDef ResolveField(this IMemberRefResolver self, MemberRef memberRef) => self.Resolve(memberRef) as FieldDef; /// /// Resolves a field /// /// this /// A field reference /// A instance or null if it couldn't be resolved. /// If the field couldn't be resolved public static FieldDef ResolveFieldThrow(this IMemberRefResolver self, MemberRef memberRef) { if (self.Resolve(memberRef) is FieldDef field) return field; throw new MemberRefResolveException($"Could not resolve field: {memberRef} ({memberRef?.GetDefinitionAssembly()})"); } /// /// Resolves a method /// /// this /// A method reference /// A instance or null if it couldn't be resolved. public static MethodDef ResolveMethod(this IMemberRefResolver self, MemberRef memberRef) => self.Resolve(memberRef) as MethodDef; /// /// Resolves a method /// /// this /// A method reference /// A instance or null if it couldn't be resolved. /// If the method couldn't be resolved public static MethodDef ResolveMethodThrow(this IMemberRefResolver self, MemberRef memberRef) { if (self.Resolve(memberRef) is MethodDef method) return method; throw new MemberRefResolveException($"Could not resolve method: {memberRef} ({memberRef?.GetDefinitionAssembly()})"); } } }