示例#1
0
        public bool IsRegistered(Type type, string instanceName = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var key = new MappingKey(type, instanceName);

            return(mappings.ContainsKey(key));
        }
示例#2
0
        private Type LookUpDependency(Type type, string instanceName)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var key = new MappingKey(type, instanceName);

            if (mappings.TryGetValue(key, out var createInstance))
            {
                var instance = createInstance();
                return(instance.GetType());
            }

            const string errorMessageFormat = "Could not find mapping for type '{0}'";

            throw new InvalidOperationException(string.Format(errorMessageFormat, type.FullName));
        }
示例#3
0
        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));
        }
示例#4
0
        public void Register(Type type, Func <object> createInstanceDelegate, string instanceName = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (createInstanceDelegate == null)
            {
                throw new ArgumentNullException(nameof(createInstanceDelegate));
            }

            var key = new MappingKey(type, instanceName);

            if (mappings.ContainsKey(key))
            {
                const string errorMessageFormat = "The requested mapping already exists '{0}'";
                throw new InvalidOperationException(errorMessageFormat);
            }

            mappings.Add(key, createInstanceDelegate);
        }