private static void BuildResolveEnumerable <T>(IBuilderContext context)
        {
            if (null == context.Existing)
            {
                var key          = context.BuildKey;
                var type         = key.Type;
                var itemType     = type.GetTypeInfo().GenericTypeArguments[0];
                var itemTypeInfo = itemType.GetTypeInfo();
                var container    = context.Container ?? context.NewBuildUp <IUnityContainer>();

                if (itemTypeInfo.IsGenericTypeDefinition)
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                              Resources.MustHaveOpenGenericType,
                                                              itemType.GetTypeInfo().Name));
                }

                var generic = new Lazy <Type>(() => itemType.GetGenericTypeDefinition());
                IEnumerable <object> enumerable = container.Registrations
                                                  .Where(r => r.RegisteredType == itemType || (itemTypeInfo.IsGenericType &&
                                                                                               r.RegisteredType.GetTypeInfo().IsGenericTypeDefinition&&
                                                                                               r.RegisteredType == generic.Value))
                                                  .Select(r => context.NewBuildUp(new NamedTypeBuildKey(itemType, r.Name)));
                context.Existing      = CastMethod.MakeGenericMethod(itemType).Invoke(null, new object[] { enumerable });
                context.BuildComplete = true;
            }

            // match the behavior of DynamicMethodConstructorStrategy
            DynamicMethodConstructorStrategy.SetPerBuildSingleton(context);
        }
コード例 #2
0
        public void BuildUp(IBuilderContext context)
        {
            Guard.ArgumentNotNull(context, "context");

            if (context.Existing == null)
            {
                var currentContainer = context.Container ?? context.NewBuildUp <IUnityContainer>();

                Type   typeToBuild = GetTypeToBuild(context.BuildKey.Type);
                string nameToBuild = context.BuildKey.Name;

                Delegate resolveMethod;

                if (IsResolvingIEnumerable(typeToBuild))
                {
                    resolveMethod = CreateResolveAllResolver(currentContainer, typeToBuild);
                }
                else
                {
                    resolveMethod = CreateResolver(currentContainer, typeToBuild, nameToBuild);
                }

                context.Existing = resolveMethod;

                DynamicMethodConstructorStrategy.SetPerBuildSingleton(context);
            }
        }
コード例 #3
0
        private static void BuildResolveAllLazy <T>(IBuilderContext context)
        {
            if (context.Existing == null)
            {
                var container = context.Container ?? context.NewBuildUp <IUnityContainer>();
                context.Existing = new Lazy <IEnumerable <T> >(() => container.ResolveAll <T>());
            }

            // match the behavior of DynamicMethodConstructorStrategy
            DynamicMethodConstructorStrategy.SetPerBuildSingleton(context);
        }
コード例 #4
0
        /// <summary>
        /// Creates an instance of this build plan's type, or fills
        /// in the existing type if passed in.
        /// </summary>
        /// <param name="context">Context used to build up the object.</param>
        public void BuildUp(IBuilderContext context)
        {
            Unity.Utility.Guard.ArgumentNotNull(context, "context");

            if (context.Existing == null)
            {
                var currentContainer = context.Container ?? context.NewBuildUp <IUnityContainer>();
                context.Existing = factory(currentContainer, context.BuildKey.Type, context.BuildKey.Name);

                DynamicMethodConstructorStrategy.SetPerBuildSingleton(context);
            }
        }
コード例 #5
0
        private static void BuildResolveLazy <T>(IBuilderContext context)
        {
            if (context.Existing == null)
            {
                var name      = context.BuildKey.Name;
                var container = context.NewBuildUp <IUnityContainer>();
                context.Existing = new Lazy <T>(() => container.Resolve <T>(name));
            }

            // match the behavior of DynamicMethodConstructorStrategy
            DynamicMethodConstructorStrategy.SetPerBuildSingleton(context);
        }