Example #1
0
        public static int Run(RegAsmOptions options)
        {
            s_Options = options;

            int RetCode = SuccessReturnCode;

            try
            {
                // Load the assembly.
                Assembly asm = null;
                try
                {
                    asm = Assembly.LoadFrom(s_Options.m_strAssemblyName);
                }
                catch (BadImageFormatException)
                {
                    throw new ApplicationException(Resource.FormatString("Err_InvalidAssembly", s_Options.m_strAssemblyName));
                }
                catch (FileNotFoundException)
                {
                    throw new ApplicationException(Resource.FormatString("Err_InputFileNotFound", s_Options.m_strAssemblyName));
                }

                if (s_Options.m_strRegFileName != null)
                {
                    // Make sure the registry file will not overwrite the input file.
                    if (String.Compare(s_Options.m_strAssemblyName, s_Options.m_strRegFileName, true, CultureInfo.InvariantCulture) == 0)
                    {
                        throw new ApplicationException(Resource.FormatString("Err_RegFileWouldOverwriteInput"));
                    }

                    // If /codebase is specified, then give a warning if the assembly is not strongly
                    // named.
                    if (s_Options.m_bSetCodeBase && asm.GetName().GetPublicKey() == null || s_Options.m_bSetCodeBase && asm.GetName().GetPublicKey().Length == 0)
                    {
                        WriteWarningMsg(Resource.FormatString("Wrn_CodeBaseWithNoStrongName"));
                    }

                    // The user wants to generate a reg file.
                    bool bRegFileGenerated = GenerateRegFile(s_Options.m_strRegFileName, asm);
                    if (!s_Options.m_bSilentMode)
                    {
                        if (bRegFileGenerated)
                        {
                            Console.WriteLine(Resource.FormatString("Msg_RegScriptGenerated", s_Options.m_strRegFileName));
                        }
                        else
                        {
                            WriteWarningMsg(Resource.FormatString("Wrn_NoRegScriptGenerated"));
                        }
                    }
                }
                else if (s_Options.m_bRegister)
                {
                    // If /codebase is specified, then give a warning if the assembly is not strongly
                    // named.
                    if (s_Options.m_bSetCodeBase && asm.GetName().GetPublicKey() == null || s_Options.m_bSetCodeBase && asm.GetName().GetPublicKey().Length == 0)
                    {
                        WriteWarningMsg(Resource.FormatString("Wrn_CodeBaseWithNoStrongName"));
                    }

                    // Register the types inside the assembly.
                    AssemblyRegistrationFlags flags = s_Options.m_bSetCodeBase ? AssemblyRegistrationFlags.SetCodeBase : 0;
                    bool bTypesRegistered           = s_RegistrationServices.RegisterAssembly(asm, flags);
                    if (!s_Options.m_bSilentMode)
                    {
                        if (bTypesRegistered)
                        {
                            Console.WriteLine(Resource.FormatString("Msg_TypesRegistered"));
                        }
                        else
                        {
                            WriteWarningMsg(Resource.FormatString("Wrn_NoTypesRegistered"));
                        }
                    }

                    // Register the typelib if the /tlb option is specified.
                    if (s_Options.m_strTypeLibName != null)
                    {
                        RegisterMainTypeLib(asm);
                    }
                }
                else
                {
                    // Unregister the types inside the assembly.
                    bool bTypesUnregistered = s_RegistrationServices.UnregisterAssembly(asm);
                    if (!s_Options.m_bSilentMode)
                    {
                        if (bTypesUnregistered)
                        {
                            Console.WriteLine(Resource.FormatString("Msg_TypesUnRegistered"));
                        }
                        else
                        {
                            WriteWarningMsg(Resource.FormatString("Wrn_NoTypesUnRegistered"));
                        }
                    }

                    // Un-register the typelib if the /tlb option is specified.
                    if (s_Options.m_strTypeLibName != null)
                    {
                        // Check to see if the assembly is imported from COM.
                        if (IsAssemblyImportedFromCom(asm))
                        {
                            if (!s_Options.m_bSilentMode)
                            {
                                WriteWarningMsg(Resource.FormatString("Wrn_ComTypelibNotUnregistered"));
                            }
                        }
                        else
                        {
                            // Unregister the typelib.
                            UnRegisterMainTypeLib();
                        }
                    }
                }
            }
            catch (TargetInvocationException e)
            {
                WriteErrorMsg(Resource.FormatString("Err_ErrorInUserDefFunc") + e.InnerException);
                RetCode = ErrorReturnCode;
            }
            catch (ReflectionTypeLoadException e)
            {
                int         i;
                Exception[] exceptions;
                WriteErrorMsg(Resource.FormatString("Err_TypeLoadExceptions"));
                exceptions = e.LoaderExceptions;
                for (i = 0; i < exceptions.Length; i++)
                {
                    try
                    {
                        Console.Error.WriteLine(Resource.FormatString("Msg_DisplayException", i, exceptions[i]));
                    }
                    catch (Exception ex)
                    {
                        Console.Error.WriteLine(Resource.FormatString("Msg_DisplayNestedException", i, ex));
                    }
                }
                RetCode = ErrorReturnCode;
            }
            catch (Exception e)
            {
                WriteErrorMsg(null, e);
                RetCode = ErrorReturnCode;
            }

            return(RetCode);
        }
 public int Run(RegAsmOptions s_options)
 {
     return(RegCode.Run(s_options));
 }