/// <summary> /// Tries to resolve a type from a preference. /// </summary> /// <param name="moduleWeaver">The module weaver to use.</param> /// <param name="fullName">The fullname of the type.</param> /// <returns>A type reference.</returns> protected static TypeReference TryResolveFromScannedAssemblies(ModuleWeaver moduleWeaver, string fullName) { if (moduleWeaver is null) { throw new System.ArgumentNullException(nameof(moduleWeaver)); } try { var type = moduleWeaver.FindTypeDefinition(fullName); var result = moduleWeaver.ModuleDefinition.ImportReference(type); return(result); } catch { moduleWeaver.WriteWarning(string.Format(CultureInfo.CurrentCulture, Resources.AssemblyOrTypeNotFoundScan, fullName)); throw; } }
/// <summary> /// Tries to resolve a type from a preference. /// </summary> /// <param name="moduleWeaver">The module weaver to use.</param> /// <param name="fullName">The fullname of the type.</param> /// <param name="assemblyName">The assembly name.</param> /// <param name="throwOnFailure">Throw an exception on failure.</param> /// <returns>A type reference.</returns> protected static TypeReference TryResolveFromReference(ModuleWeaver moduleWeaver, string fullName, string assemblyName, bool throwOnFailure = true) { if (moduleWeaver is null) { throw new System.ArgumentNullException(nameof(moduleWeaver)); } try { var assembly = moduleWeaver.ModuleDefinition.AssemblyResolver.Resolve(moduleWeaver.ModuleDefinition.AssemblyReferences.FirstOrDefault(asm => asm.Name == assemblyName)); return(moduleWeaver.ModuleDefinition.ImportReference(assembly.MainModule.GetType(fullName))); } catch { if (throwOnFailure) { moduleWeaver.WriteWarning(string.Format(CultureInfo.CurrentCulture, Resources.AssemblyOrTypeNotFoundReferences, fullName, assemblyName)); throw; } return(null); } }