/// <summary> /// Resolves a reference during the exporting process. This means that the /// type library references the type library in another assembly. We must /// call back our <see cref="TypeLibHelper"/> to do this. /// </summary> /// <param name="assembly">Assembly to resolve.</param> /// <returns>Type library for the specified <paramref name="assembly"/>. /// Must implement the <see cref="ITypeLib"/> interface.</returns> /// <exception cref="ArgumentNullException"><paramref name="assembly"/> /// is <c>null</c>.</exception> public object ResolveRef(Assembly assembly) { ITypeLib tlb = null; // Validate input. if (assembly == null) { throw new ArgumentNullException("assembly"); } // We'll export the type lib next to the assembly. string assemblyName = assembly.GetName().Name; string typeLibName = Path.ChangeExtension(Path.Combine(Path.GetDirectoryName( AssemblyTools.GetAssemblyPath(assembly)), assemblyName), ".tlb"); // Log recursive references in verbose mode. parent.console.Verbose.WriteLine("Recursively exporting and registering type " + "library of assembly \"{0}\" to \"{1}\".", assemblyName, typeLibName); // Call back parent to export and register the type lib. tlb = parent.ExportTypeLibrary(assembly, typeLibName); parent.RegisterTypeLibrary(tlb, typeLibName); Debug.Assert(tlb != null); return(tlb); }
/// <summary> /// Exports the type library of the given assembly, then registers /// the type library. Will recursively export and register TLBs of /// referenced assemblies as needed. /// </summary> /// <param name="assembly"><see cref="System.Reflection.Assembly"/> /// to export. Cannot be <c>null</c>.</param> /// <param name="typeLibName">Full path to type library to export. /// Cannot be <c>null</c>, empty or equal to the path of /// <paramref name="assembly"/>.</param> /// <exception cref="ArgumentNullException"><paramref name="assembly"/> /// or <paramref name="typeLibName"/> is <c>null</c>.</exception> /// <exception cref="ArgumentException"><paramref name="typeLibName"/> /// is <see cref="String.Empty"/>. /// /// OR /// /// <paramref name="typeLibName"/> is equal to the local path /// of <paramref name="assembly"/>.</exception> public void ExportAndRegisterTypeLibrary(Assembly assembly, string typeLibName) { // Validate input. if (assembly == null) { throw new ArgumentNullException("assembly"); } if (typeLibName == null) { throw new ArgumentNullException("typeLibName"); } if (typeLibName.Length == 0) { throw new ArgumentException("Type library name cannot be empty.", "typeLibName"); } string assemblyName = AssemblyTools.GetAssemblyPath(assembly); if (String.Compare(assemblyName, typeLibName, true) == 0) { throw new ArgumentException("Type library cannot overwrite assembly.", "typeLibName"); } // Export the type lib, then register it. ITypeLib tlb = ExportTypeLibrary(assembly, typeLibName); RegisterTypeLibrary(tlb, typeLibName); // Log normal message to tell we've registered the type lib. console.WriteLine("Type library \"{0}\" exported from assembly \"{1}\" " + "and registered successfully.", assemblyName, typeLibName); }
/// <summary> /// Exports the type library of the given assembly to the given path. /// Will recursively export and register TLBs of referenced assemblies /// as needed. /// </summary> /// <param name="assembly"><see cref="System.Reflection.Assembly"/> /// to export. Cannot be <c>null</c>.</param> /// <param name="typeLibName">Full path to type library to export. /// Cannot be <c>null</c>, empty or equal to the path of /// <paramref name="assembly"/>.</param> /// <returns>Type library reference.</returns> /// <exception cref="CLRegAsmException">Failed to save type library /// <paramref name="typeLibName"/> to disk.</exception> private ITypeLib ExportTypeLibrary(Assembly assembly, string typeLibName) { Debug.Assert(assembly != null); Debug.Assert(!String.IsNullOrEmpty(typeLibName)); Debug.Assert(String.Compare(AssemblyTools.GetAssemblyPath(assembly), typeLibName, true) != 0); // Create type lib converter and export the type lib. // This will call us back recursively if referenced assemblies' // type libs need to be exported and registered also. TypeLibConverter tlbConverter = new TypeLibConverter(); ITypeLib tlb = (ITypeLib)tlbConverter.ConvertAssemblyToTypeLib(assembly, typeLibName, flags, new ConverterCallback(this)); // Save all changes, which will save the data to disk. try { (tlb as ICreateTypeLib).SaveAllChanges(); } catch (Exception e) { throw new CLRegAsmException(String.Format("Could not save type library \"{0}\" to disk", typeLibName), e); } Debug.Assert(tlb != null); return(tlb); }