public FrmCustomUpdateCreator() { InitializeComponent(); customUpdateElementViewer1.ElementDoubleClick += new CustomUpdateElementViewer.CustomUpdateElementViewer.ElementDoubleClickEventHandler(addElement); ExecutableElement executableElement = new ExecutableElement(); customUpdateElementViewer1.AddElement(executableElement); ScriptElement scriptElement = new ScriptElement(); customUpdateElementViewer1.AddElement(scriptElement); TextFileElement textFileElement = new TextFileElement(); customUpdateElementViewer1.AddElement(textFileElement); FileElement fileElement = new FileElement(); customUpdateElementViewer1.AddElement(fileElement); FolderElement folderElement = new FolderElement(); customUpdateElementViewer1.AddElement(folderElement); ServiceElement serviceElement = new ServiceElement(); customUpdateElementViewer1.AddElement(serviceElement); RegistryKeyElement RegKeyElement = new RegistryKeyElement(); customUpdateElementViewer1.AddElement(RegKeyElement); RegistryElement RegElement = new RegistryElement(); customUpdateElementViewer1.AddElement(RegElement); VariableElement variableElement = new VariableElement(); customUpdateElementViewer1.AddElement(variableElement); PowerManagementElement powerElement = new PowerManagementElement(); customUpdateElementViewer1.AddElement(powerElement); WaitElement waitElement = new WaitElement(); customUpdateElementViewer1.AddElement(waitElement); KillProcessElement killElement = new KillProcessElement(); customUpdateElementViewer1.AddElement(killElement); ReturnCodeElement returnCodeElement = new ReturnCodeElement(); customUpdateElementViewer1.AddElement(returnCodeElement); lnkLblHelp.Enabled = System.IO.File.Exists("Custom Updates.pdf"); }
/// <summary> /// Retrieve an instance of the element registered with the highest pripority. /// </summary> /// <param name="instanceType">Instance type.</param> /// <param name="args">Arguments to create the instance.</param> /// <typeparam name="TInterface">The interface to query.</typeparam> public virtual TInterface Retrieve <TInterface> (InstanceType instanceType = InstanceType.New, params object [] args) { CheckInterfaceExists(typeof(TInterface)); RegistryElement element = elements [typeof(TInterface)].OrderByDescending(e => e.Priority).First(); return(GetInstance <TInterface> (element, instanceType, args)); }
/// <summary> /// Register the specified interfac, type and priority in runtime. /// </summary> /// <param name="interfac">Interfac.</param> /// <param name="type">Type.</param> /// <param name="priority">Priority.</param> public void Register(Type interfac, Type type, int priority = 0) { if (!elements.ContainsKey(interfac)) { elements [interfac] = new List <RegistryElement> (); } RegistryElement element = new RegistryElement(type, priority); AddElement(interfac, element); Log.Information(string.Format("Registered {0} in {1} with priority {2}", type, interfac, priority)); }
void AddElement(Type interfac, RegistryElement element) { int index = elements [interfac].FindIndex(elem => elem.Priority == element.Priority); if (index != -1) { Log.Error("Two items registered with the same priority." + $"Replacing {elements [interfac] [index]} with {element}"); elements [interfac] [index] = element; } else { elements [interfac].Add(element); } }
T GetInstance <T> (RegistryElement element, InstanceType instanceType, params object [] args) { T instance; if (instanceType == InstanceType.New || instanceType == InstanceType.Default && element.Instance == null) { instance = (T)Activator.CreateInstance(element.Type, args); if (instanceType == InstanceType.Default) { element.Instance = instance; } } else { instance = (T)element.Instance; } return(instance); }
/// <summary> /// register a new element with an instance object for a given interface with a priority in runtime /// </summary> /// <param name="interfac">Interfac.</param> /// <param name="instance">The instance of the registered interface.</param> /// <param name="priority">Priority.</param> public void Register(Type interfac, object instance, int priority = 0) { if (!interfac.IsAssignableFrom(instance.GetType())) { throw new InvalidOperationException("Registered instance does not implement the proper interface"); } if (!elements.ContainsKey(interfac)) { elements [interfac] = new List <RegistryElement> (); } RegistryElement element = new RegistryElement(instance.GetType(), priority); element.Instance = instance; AddElement(interfac, element); Log.Information(string.Format("Registered instance of type {0} in {1} with priority {2}", instance.GetType(), interfac, priority)); }