public void Register(Type type, Func <object> createInstanceDelegate, bool singleton = false, bool initialize = false, string instanceName = null) { if (type == null) { throw new ArgumentNullException("type"); } if (createInstanceDelegate == null) { throw new ArgumentNullException("createInstanceDelegate"); } var key = new MappingKey(type, singleton, instanceName); if (!_mappings.ContainsKey(key)) { if (initialize) { if (singleton) { key.Instance = createInstanceDelegate(); } else { createInstanceDelegate(); } } _mappings.Add(key, createInstanceDelegate); } }
public void Deregister(Type type, string instanceName = null) { var key = new MappingKey(type, default(bool), instanceName); Func <object> obj; if (_mappings.TryGetValue(key, out obj)) { _mappings.Remove(_mappings.FirstOrDefault(x => x.Value == obj).Key); } }
public object Resolve(Type type, string instanceName = null) { var key = new MappingKey(type, default(bool), instanceName); Func <object> obj; if (_mappings.TryGetValue(key, out obj)) { var mk = _mappings.FirstOrDefault(x => x.Value == obj).Key; if (mk.Singleton) { return(mk.Instance ?? (mk.Instance = obj())); } return(obj()); } throw new InvalidOperationException(string.Format("Could not find mapping for type '{0}'", type.FullName)); }