コード例 #1
0
        /// <inheritdoc />
        public IBindingData BindSingleton(string serviceName, Type implType, params PropertyInjection[] propertyInjections)
        {
            GuardNotDisposingOrDisposed();
            Guard.RequireNotNullOrEmpty <ArgumentException>(serviceName, $"Invalid '{nameof(serviceName)}'.");
            GuardUnbound(Dealias(serviceName));
            GuardImplType(implType);
            var bindingData = new BindingData(this)
            {
                ServiceName      = serviceName,
                ImplType         = implType,
                LifeCycleManaged = true,
            };
            int propertyInjectionIndex = 0;

            foreach (var propertyInjection in propertyInjections)
            {
                if (string.IsNullOrEmpty(propertyInjection.PropertyName))
                {
                    throw new ArgumentException($"Property injection {propertyInjectionIndex} has a invalid property name.");
                }

                if (propertyInjection.Value == null)
                {
                    throw new ArgumentException($"Property injection {propertyInjectionIndex} has a null value.");
                }

                var propertyInfo = implType.GetProperty(propertyInjection.PropertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
                if (propertyInfo == null)
                {
                    throw new ArgumentException($"Cannot find property named '{propertyInjection.PropertyName}' at index {propertyInjectionIndex}.");
                }

                if (!propertyInfo.PropertyType.IsInstanceOfType(propertyInjection.Value))
                {
                    throw new ArgumentException($"Property injection {propertyInjectionIndex} has a value that doesn't have a feasible type.");
                }

                bindingData.AddPropertyInjection(propertyInjection);
                propertyInjectionIndex++;
            }

            m_ServiceNameToBindingDataMap[serviceName] = bindingData;
            return(bindingData);
        }