private EmulatorComponent UpdateEmulatorComponent(Type type, String componentId, String updatedId, XmlReader config)
        {
            EmulatorComponent updated = _emulator.FindComponentById(updatedId);

            if (updated == null)
            {
                throw new Exception("Cannot find component " + updatedId + ".");
            }

            if (type.IsAssignableFrom(updated.GetType()) == false)
            {
                throw new Exception("Cannot reconfigure a " + updated.GetType().ToString() + " into a " + type.ToString());
            }

            EmulatorComponent original = updated.Clone();

            updated.Configure(config);

            if (updated.IsReplaceableBy(original) == false)
            {
                // the crucial ID info has changed
                throw new Exception("The identification information of " + updated.GetType().ToString() + " cannot be updated in " + updatedId + ".");
            }

            if (componentId != null)
            {
                updated.ComponentId = componentId;
            }

            return(updated);
        }
        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);
        }