public bool IsRegistered(Type type, string instanceName = null) { if (type == null) { throw new ArgumentNullException("type"); } var key = new MappingKey(type, instanceName); return(_mappings.ContainsKey(key)); }
public object Resolve(Type type, string instanceName = null) { var key = new MappingKey(type, instanceName); Func <object> createInstance; if (_mappings.TryGetValue(key, out createInstance)) { var instance = createInstance(); return(instance); } const string errorMessageFormat = "Could not find mapping for type '{0}'"; throw new InvalidOperationException(string.Format(errorMessageFormat, type.FullName)); }
/// <summary> /// Determines whether the specified object is equal to the current object. /// </summary> /// <param name="obj">The object to compare with the current object</param> /// <returns> /// <c>true</c> if the specified object is equal to the current object; otherwise, <c>false</c>. /// </returns> public override bool Equals(object obj) { if (obj == null) { return(false); } MappingKey compareTo = obj as MappingKey; if (ReferenceEquals(this, compareTo)) { return(true); } if (compareTo == null) { return(false); } return(Type.Equals(compareTo.Type) && string.Equals(InstanceName, compareTo.InstanceName, StringComparison.InvariantCultureIgnoreCase)); }
public void Register(Type type, Func <object> createInstance, string instanceName = null) { if (type == null) { throw new ArgumentNullException("type"); } if (createInstance == null) { throw new ArgumentNullException("createInstanceDelegate"); } var key = new MappingKey(type, instanceName); if (_mappings.ContainsKey(key)) { const string errorMessageFormat = "The requested mapping already exists - {0}"; throw new InvalidOperationException(string.Format(errorMessageFormat, key.ToTraceString())); } _mappings.Add(key, createInstance); }