public static string GenerateAssemblyFromTypeLib(UCOMITypeLib typLib) { // Need a sink for the TypeLibConverter. ImporterNotiferSink sink = new ImporterNotiferSink(); #region Notes on Strong Name... /* Don't need a *.snk file if you are not building a primary interop asm. * Just send in nulls to the ConvertTypeLibToAssembly() method. * But if you have a valid *.snk file, you can use it as so: * * // Object representation of *.snk file. * FileStream fs = File.Open(@"D:\APress Books\InteropBook\MyTypeLibImporter\bin\Debug\theKey.snk", * FileMode.Open); * System.Reflection.StrongNameKeyPair keyPair = new StrongNameKeyPair(fs); */ #endregion // This class will covert COM type info into // .NET metadata and vice-versa. TypeLibConverter tlc = new TypeLibConverter(); // Generate name of the assembly. string typeLibName = Marshal.GetTypeLibName(typLib); string asmName = "interop." + typeLibName + ".dll"; // Now make the assembly based on COM type information. AssemblyBuilder asmBuilder = tlc.ConvertTypeLibToAssembly((UCOMITypeLib)typLib, asmName, TypeLibImporterFlags.SafeArrayAsSystemArray, sink, null, // If you have a strong name: keyPair.PublicKey, null, // If you have a strong name: keyPair typeLibName, // Namespace name is same as file name. null); // null = (typeLibMajor.typeLibMinor.0.0) // Save the assembly in the app directory! asmBuilder.Save(asmName); // return Assembly ref to call. return(asmName); }
public static string GenerateAssemblyFromTypeLib(UCOMITypeLib typLib) { // Need a sink for the TypeLibConverter. ImporterNotiferSink sink = new ImporterNotiferSink(); #region Notes on Strong Name... /* Don't need a *.snk file if you are not building a primary interop asm. * Just send in nulls to the ConvertTypeLibToAssembly() method. * But if you have a valid *.snk file, you can use it as so: * * // Object representation of *.snk file. * FileStream fs = File.Open(@"D:\APress Books\InteropBook\MyTypeLibImporter\bin\Debug\theKey.snk", * FileMode.Open); * System.Reflection.StrongNameKeyPair keyPair = new StrongNameKeyPair(fs); */ #endregion // This class will covert COM type info into // .NET metadata and vice-versa. TypeLibConverter tlc = new TypeLibConverter(); // Generate name of the assembly. string typeLibName = Marshal.GetTypeLibName(typLib); string asmName = "interop." + typeLibName + ".dll"; // Now make the assembly based on COM type information. AssemblyBuilder asmBuilder = tlc.ConvertTypeLibToAssembly((UCOMITypeLib)typLib, asmName, TypeLibImporterFlags.SafeArrayAsSystemArray, sink, null, // If you have a strong name: keyPair.PublicKey, null, // If you have a strong name: keyPair typeLibName, // Namespace name is same as file name. null); // null = (typeLibMajor.typeLibMinor.0.0) // Save the assembly in the app directory! asmBuilder.Save(asmName); // return Assembly ref to call. return asmName; }