/// <summary> /// Looks for a method with the UnRegisterFunctionAttribute /// </summary> /// <param name="type">the type you want looking for the method</param> /// <param name="method">the method when its found</param> /// <param name="attribute">the attribute when its found</param> /// <returns>true when the method was found</returns> public static bool GetUnRegisterAttribute(Type type, ref MethodInfo method, ref UnRegisterFunctionAttribute attribute) { foreach (MethodInfo item in type.GetMethods(BindingFlags.Static | BindingFlags.Public)) { object[] array = item.GetCustomAttributes(typeof(UnRegisterFunctionAttribute), false); if (array.Length == 1) { method = item; attribute = array[0] as UnRegisterFunctionAttribute; return true; } } return false; }
/// <summary> /// Derived Unregister Call Helper /// </summary> /// <param name="type">type for derived class</param> /// <param name="registerMethod">the method to call</param> /// <param name="registerAttribute">arguments</param> private static void CallDerivedUnRegisterMethod(Type type, MethodInfo registerMethod, UnRegisterFunctionAttribute registerAttribute) { if (registerAttribute.Value == RegisterMode.Replace) registerMethod.Invoke(null, new object[] { type, RegisterCall.Replace }); else if (registerAttribute.Value == RegisterMode.CallBeforeAndAfter || registerAttribute.Value == RegisterMode.CallBefore) registerMethod.Invoke(null, new object[] { type, RegisterCall.CallBefore }); }
/// <summary> /// Do unregister process /// </summary> /// <param name="type">addin type</param> /// <param name="addinOfficeRegistryKey">office application registry path</param> /// <param name="scope">the current installation scope</param> /// <param name="keyState">the office registry key need to delete</param> public static void Proceed(Type type, string[] addinOfficeRegistryKey, InstallScope scope, OfficeUnRegisterKeyState keyState) { try { MethodInfo registerMethod = null; UnRegisterFunctionAttribute registerAttribute = null; bool registerMethodPresent = AttributeReflector.GetUnRegisterAttribute(type, ref registerMethod, ref registerAttribute); if ((null != registerAttribute && true == registerMethodPresent) && (registerAttribute.Value == RegisterMode.CallBefore || registerAttribute.Value == RegisterMode.CallBeforeAndAfter)) { if (!CallDerivedUnRegisterMethod(registerMethod, type, registerAttribute.Value == RegisterMode.Replace ? RegisterCall.Replace : RegisterCall.CallBefore, scope, keyState)) { if (!RegisterErrorHandler.RaiseStaticErrorHandlerMethod(type, RegisterErrorMethodKind.UnRegister, new UnregisterException())) { return; } } if (registerAttribute.Value == RegisterMode.Replace) { return; } } ProgIdAttribute progId = AttributeReflector.GetProgIDAttribute(type); RegistryLocationAttribute location = AttributeReflector.GetRegistryLocationAttribute(type); CodebaseAttribute codebase = AttributeReflector.GetCodebaseAttribute(type); ProgrammableAttribute programmable = AttributeReflector.GetProgrammableAttribute(type); bool isSystemComponent = location.IsMachineComponentTarget(scope); bool isSystemAddin = location.IsMachineAddinTarget(scope); if (null != programmable) { ProgrammableAttribute.DeleteKeys(type.GUID, isSystemComponent, false); } if (null != codebase && codebase.Value == true) { Assembly thisAssembly = Assembly.GetAssembly(type); string assemblyVersion = thisAssembly.GetName().Version.ToString(); CodebaseAttribute.DeleteValue(type.GUID, isSystemComponent, assemblyVersion, false); } if (keyState == OfficeUnRegisterKeyState.NeedToDelete) { foreach (string item in addinOfficeRegistryKey) { RegistryLocationAttribute.TryDeleteApplicationKey(isSystemAddin, item, progId.Value); } } if ((null != registerAttribute && true == registerMethodPresent) && (registerAttribute.Value == RegisterMode.CallAfter || registerAttribute.Value == RegisterMode.CallBeforeAndAfter)) { if (!CallDerivedUnRegisterMethod(registerMethod, type, RegisterCall.CallAfter, scope, keyState)) { RegisterErrorHandler.RaiseStaticErrorHandlerMethod(type, RegisterErrorMethodKind.UnRegister, new UnregisterException()); } } } catch (System.Exception exception) { NetOffice.DebugConsole.Default.WriteException(exception); if (!RegisterErrorHandler.RaiseStaticErrorHandlerMethod(type, RegisterErrorMethodKind.UnRegister, exception)) { throw; } } }