/// <summary> /// Gets a previously registered object by key. /// </summary> /// <returns> /// The matching object if any /// </returns> /// <param name='key'> /// Key identifying object /// </param> /// <typeparam name='TType'> /// The type of object to retrieve /// </typeparam> public TType GetByKey <TType>(object key) where TType : SimulationElement { SimulationElement objectToRetrieve = null; foreach (var entry in _registeredElements) { if (IsTypeEqualOrSubclass(entry.Key, typeof(TType))) { if (entry.Value.TryGetValue(key, out objectToRetrieve)) { break; } } } return(objectToRetrieve as TType); }
/// <summary> /// Register an object with this context /// </summary> public void Register(Type typeToRegister, object objectToRegister) { if (!IsTypeEqualOrSubclass(typeToRegister, typeof(SimulationElement))) { throw new ArgumentException("typeToRegister"); } SimulationElement element = objectToRegister as SimulationElement; Dictionary <object, SimulationElement> elementsByKey = null; if (!_registeredElements.TryGetValue(typeToRegister, out elementsByKey)) { elementsByKey = new Dictionary <object, SimulationElement>(); _registeredElements[typeToRegister] = elementsByKey; } elementsByKey[element.Key] = element; }