Пример #1
0
        public ILocatorResolutionInfo <TClass> RegisterType <TInterface, TClass>(string name) where TClass : TInterface
        {
            var interfaceType = typeof(TInterface);
            var classType     = typeof(TClass);

            if (interfaceType.IsGenericType || classType.IsGenericType)
            {
                throw new TypeNotSupportedException(interfaceType.FullName, "Generic Types cannot be registered by RegisterType registrationInfo registration");
            }

            var key = GetKey(interfaceType, name);

            CheckDuplicated(key);

            var propRegistrationInfo = new PropertyRegistrationInfo <TClass>();

            var typeBuilder = new LocatorTypeBuilder <TClass>(this, propRegistrationInfo);

            registeredTypes.AddOrUpdate(key, typeBuilder, (pair, builder) => typeBuilder);

            var registrationInfo = new LocatorRegistrationInfo <TClass>();

            registrationInfo.PropertyInjectionResolvers = typeBuilder.PropertyResolvers;

            return(new LocatorResolutionInfo <TClass>(registrationInfo, propRegistrationInfo));
        }
Пример #2
0
        protected virtual bool TryConstructType(Type type, string name, out object value)
        {
            var         key = GetKey(type, name);
            TypeBuilder registration;

            if (!registeredTypes.ContainsKey(key))
            {
#if RESOLVE_NON_REGISTERED
                registration = new LocatorTypeBuilder(this, type, name);
#else
                value = TypeHelpers.GetDefault(@type);
                return(false);
#endif
            }
            else
            {
                registration = registeredTypes[key];
            }

            if (registration == null)
            {
                throw new TypeNotResolvedException(type.FullName, "Cannot find type registration");
            }

            var ctor = TypeHelpers.TryGetConstructor(registration.DestType);

            registration.CreateBuildingContext();
            registration.InitBuildingContext();
            //value types have no default constructor and should be initalized by default value
            if (ctor == null && registration.DestType.IsValueType)
            {
                value = TypeHelpers.GetDefault(registration.DestType);
            }
            else
            {
                registration.CreateInstance(ctor, type.FullName);
                value = registration.Context.Instance;
            }
            registration.InjectTypeProperties();
            return(true);
        }