public void RegisterComponent(EmulatorComponent component) { if (!_components.Contains(component)) { if (FindComponentById(component.ComponentId) != null) { throw new Exception("Another emulator component with component ID \"" + component.ComponentId + "\" already exists."); } component.SetEmulator(this); _components.Add(component); if (_emulatorState >= EmulatorState.Setup) { component.SetupComponent(); } if (_emulatorState >= EmulatorState.Initialize) { component.InitializeComponent(); EmulatorComponentCollection ecc; if ((ecc = FindCollection(component)) != null) { ecc.RegisterInternal(component); } } if (_emulatorState >= EmulatorState.ShutDown) { throw new ApplicationException("Cannot add new Emulator component during Shutdown"); } } }
private EmulatorComponent ReplaceEmulatorComponent(Type type, String componentId, String replacedId, XmlReader config) { EmulatorComponent replaced = _emulator.FindComponentById(replacedId); if (replaced == null) { throw new Exception("Cannot find the component, " + replacedId + ", to be replaced."); } if (componentId == null) { componentId = replacedId; } // Make sure the new componentId isn't taken already EmulatorComponent idMatched = _emulator.FindComponentById(componentId); if (idMatched != null) { if (idMatched != replaced) { throw new Exception("The Component ID, " + componentId + ", has already been used."); } else if (idMatched.LinkedBy != null) { throw new Exception("Cannot replace the EmulatorComponent " + idMatched.ComponentId + " here because it's linked elsewhere."); } } // Create the component and configure it EmulatorComponent component = (EmulatorComponent)Activator.CreateInstance(type); component.ComponentId = componentId; component.SetEmulator(_emulator); component.Configure(config); if (replaced.IsReplaceableBy(component) == false) { throw new Exception("The new component cannot be used to replace the old one."); } _emulator.UnregisterComponent(replaced); Debug.Assert(_emulator.FindReplaceableComponent(component) == null); _emulator.RegisterComponent(component); return(component); }
private EmulatorComponent CreateEmulatorComponent(Type type, String componentId, XmlReader config) { if (componentId != null && _emulator.FindComponentById(componentId) != null) { throw new Exception("Component, " + componentId + ", already existed."); } // Create the component and configure it EmulatorComponent component = (EmulatorComponent)Activator.CreateInstance(type); component.SetEmulator(_emulator); if (String.IsNullOrEmpty(componentId) == false) { component.ComponentId = componentId; } component.Configure(config); // Make sure the component is unique (there isn't any existing components that this new component will replace) EmulatorComponent replaceable = _emulator.FindReplaceableComponent(component); if (replaceable != null) { if (replaceable.CreatedByConfigurationFile) { throw new Exception("Another component, " + replaceable.ComponentId + ", already exists."); } else { // if the component is generated by default, then we won't enforce the replaceId requirement, and replace it. _emulator.UnregisterComponent(replaceable); } } _emulator.RegisterComponent(component); return(component); }