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 void RemoveEmulatorComponent(Type type, String removedId) { EmulatorComponent removed = _emulator.FindComponentById(removedId); if (removed == null) { throw new Exception("Cannot find component " + removedId + "."); } if (type.IsAssignableFrom(removed.GetType()) == false) { throw new Exception("Type mismatched -- " + removed.GetType().ToString() + " and " + type.ToString() + "."); } _emulator.UnregisterComponent(removed); }
// default implementation for EmulatorComponent.Configure() public void ConfigureEmulatorComponent(EmulatorComponent component, XmlReader config) { Type objType = component.GetType(); config.ReadStartElement(); // bypass the root element while (true) { config.Read(); if (config.IsStartElement()) { String propertyName = config.Name; PropertyInfo property = objType.GetProperty(propertyName); if (property == null) { throw new XmlException("Unrecognize element (Property): " + propertyName); } if (property.CanWrite == false) { throw new Exception("The element (Property), " + propertyName + ", is read-only."); } Type propertyType = property.PropertyType; Type actualType = propertyType; if (config.MoveToAttribute("type")) { actualType = ResolveType(config.Value); } config.MoveToElement(); if (typeof(EmulatorComponent).IsAssignableFrom(propertyType) || typeof(EmulatorComponent).IsAssignableFrom(actualType)) { XmlReader ecConfig = config.ReadSubtree(); EmulatorComponent ec = ProcessEmulatorComponent(actualType, ecConfig); ecConfig.Close(); if (propertyType.IsAssignableFrom(ec.GetType()) == false) { throw new Exception("Type mismatch: " + ec.GetType().ToString() + " is not a " + propertyType.ToString()); } if (ec.LinkedBy == null) { ec.LinkedBy = component; } else if (ec.LinkedBy != component) { throw new Exception("This EmulatorComponent is already in " + ec.LinkedBy.ComponentId + "."); } if (component.LinkedComponents.Contains(ec) == false) { component.LinkedComponents.Add(ec); } EmulatorComponent oldEc = (EmulatorComponent)property.GetValue(component, null); if ((oldEc != null) && (oldEc != ec)) { _emulator.UnregisterComponent(oldEc); } property.SetValue(component, ec, null); } else { XmlReader objectConfig = config.ReadSubtree(); property.SetValue(component, ParseObject(propertyType, objectConfig), null); objectConfig.Close(); } } else { break; } } }
private EmulatorComponentCollection FindCollection(EmulatorComponent ec) { List <EmulatorComponent> collections = _components.FindAll(IsDerivedFrom <EmulatorComponentCollection>); return((EmulatorComponentCollection)collections.Find( delegate(EmulatorComponent ecc) { return ((EmulatorComponentCollection)ecc).CollectionType.IsAssignableFrom(ec.GetType()); })); }
// default implementation for EmulatorComponent.Configure() public void ConfigureEmulatorComponent( EmulatorComponent component, XmlReader config ) { Type objType = component.GetType(); config.ReadStartElement(); // bypass the root element while (true) { config.Read(); if (config.IsStartElement()) { String propertyName = config.Name; PropertyInfo property = objType.GetProperty( propertyName ); if (property == null) { throw new XmlException( "Unrecognize element (Property): " + propertyName ); } if (property.CanWrite == false) { throw new Exception( "The element (Property), " + propertyName + ", is read-only." ); } Type propertyType = property.PropertyType; Type actualType = propertyType; if (config.MoveToAttribute( "type" )) { actualType = ResolveType( config.Value ); } config.MoveToElement(); if (typeof( EmulatorComponent ).IsAssignableFrom( propertyType ) || typeof( EmulatorComponent ).IsAssignableFrom( actualType )) { XmlReader ecConfig = config.ReadSubtree(); EmulatorComponent ec = ProcessEmulatorComponent( actualType, ecConfig ); ecConfig.Close(); if (propertyType.IsAssignableFrom( ec.GetType() ) == false) { throw new Exception( "Type mismatch: " + ec.GetType().ToString() + " is not a " + propertyType.ToString() ); } if (ec.LinkedBy == null) { ec.LinkedBy = component; } else if (ec.LinkedBy != component) { throw new Exception( "This EmulatorComponent is already in " + ec.LinkedBy.ComponentId + "." ); } if (component.LinkedComponents.Contains( ec ) == false) { component.LinkedComponents.Add( ec ); } EmulatorComponent oldEc = (EmulatorComponent)property.GetValue( component, null ); if ((oldEc != null) && (oldEc != ec)) { _emulator.UnregisterComponent( oldEc ); } property.SetValue( component, ec, null ); } else { XmlReader objectConfig = config.ReadSubtree(); property.SetValue( component, ParseObject( propertyType, objectConfig ), null ); objectConfig.Close(); } } else { break; } } }