//BaseController should use reflection //and not generics as we do not know the types at design time public static MHEControl CreateMHEControlGeneric <T, U>(IControllable assem, ProtocolInfo info) where T : ProtocolInfo where U : MHEControl { MHEControl protocolConfig = null; //generic plc config object try { if (info == null) { var i = (T)Activator.CreateInstance(typeof(T), null); ProtocolInfo protocolInfo = i; //generic plc config object constructor argument type protocolInfo.assem = assem.Name; object[] args = { protocolInfo, assem }; var ctr = (U)Activator.CreateInstance(typeof(U), args); protocolConfig = ctr; } else { object[] args = { info, assem }; var ctr = (U)Activator.CreateInstance(typeof(U), args); info.assem = assem.Name; protocolConfig = ctr; } } catch (Exception e) { Experior.Core.Environment.Log.Write("Can't create control!", Color.Red); Environment.Log.Write(e.Message, Color.Red); } return(protocolConfig); }
/// <summary> /// Set up the PLC then set up the protocol that the PLC uses. /// Returns the controller and the controllerProperties (out MHEControl object) for the controller type and assembly /// The Info of the MHE Component must implement the IControllableInfo interface (a comment by BG) /// </summary> /// <param name="assemblyInfo"></param> /// <param name="assembly"></param> /// <returns>An MHE control object, if the controller doesnot exist (e.g. the string "No Controller") then null is returned</returns> public static MHEControl SetMHEControl(IControllableInfo assemblyInfo, IControllable assembly) { //BaseController controller = null; IController controller = null; MHEControl controllerProperties = null; if (!Assembly.Items.ContainsKey(assemblyInfo.ControllerName)) { return(null); } //controller = Assembly.Items[assemblyInfo.ControllerName] as BaseController; controller = Assembly.Items[assemblyInfo.ControllerName] as IController; if (controller != null && controllerProperties == null) { assemblyInfo.ControllerName = controller.Name; //We need to set the controller here so that it is set when the MHEControl is created otherwise it is null assembly.Controller = controller; // controllerProperties = BaseController.CreateMHEControlGeneric<>(assembly, assemblyInfo.ProtocolInfo); controllerProperties = controller.CreateMHEControl(assembly, assemblyInfo.ProtocolInfo) as MHEControl; //Create the correct type if (controllerProperties != null) { assemblyInfo.ProtocolInfo = controllerProperties.Info; //Save the protocol info so that it can be recreated } } return(controllerProperties); }