Ejemplo n.º 1
0
        private static void BuildResolveEnumerable <T>(IBuilderContext context)
        {
            if (null == context.Existing)
            {
                var itemType     = typeof(T);
                var itemTypeInfo = itemType.GetTypeInfo();
                var container    = context.Container ?? context.NewBuildUp <IUnityContainer>();

                if (itemTypeInfo.IsGenericTypeDefinition)
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                              Constants.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;
            }

            context.SetPerBuildSingleton();
        }
Ejemplo n.º 2
0
 public void BuildUp(IBuilderContext context)
 {
     if ((context ?? throw new ArgumentNullException(nameof(context))).Existing == null)
     {
         context.Existing = _factoryFunc(context.Container, context.BuildKey.Type, context.BuildKey.Name);
         context.SetPerBuildSingleton();
     }
 }
Ejemplo n.º 3
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)
 {
     if ((context ?? throw new ArgumentNullException(nameof(context))).Existing == null)
     {
         var currentContainer = context.NewBuildUp <IUnityContainer>();
         context.Existing = factory(currentContainer, context.BuildKey.Type, context.BuildKey.Name);
         context.SetPerBuildSingleton();
     }
 }
        private static void BuildResolveAllLazy <T>(IBuilderContext context)
        {
            if (context.Existing == null)
            {
                context.Existing = new Lazy <IEnumerable <T> >(() => context.Container.ResolveAll <T>());
            }

            // match the behavior of DynamicMethodConstructorStrategy
            context.SetPerBuildSingleton();
        }
Ejemplo n.º 5
0
        public void BuildUp(IBuilderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.Existing == null)
            {
                context.Existing = _factoryFunc.Method.Invoke(_factoryFunc.Target, ResolveParams(context).ToArray());
                context.SetPerBuildSingleton();
            }
        }
Ejemplo n.º 6
0
        private static void ResolveEnumerable <T>(IBuilderContext context)
        {
            if (null != context.Existing)
            {
                return;
            }

            var container = (UnityContainer)context.Container;

            context.Existing = container.GetRegisteredNames(container, typeof(T))
                               .Select(registration => context.NewBuildUp(typeof(T), registration))
                               .Cast <T>()
                               .ToArray();

            context.BuildComplete = true;
            context.SetPerBuildSingleton();
        }
        public void BuildUp(IBuilderContext context)
        {
            if (context.Existing == null)
            {
                // Reserved type
                context.AddResolverOverrides(new ParameterOverride("type", new InjectionParameter(context.BuildKey.Type)));

                // Reserved name
                if (!string.IsNullOrWhiteSpace(context.BuildKey.Name))
                {
                    context.AddResolverOverrides(new ParameterOverride("name", new InjectionParameter(context.BuildKey.Name)));
                }

                context.Existing = _factoryFunc.DynamicInvoke(_operations.Select(p => ResolveArgument(p, context))
                                                              .ToArray());

                context.SetPerBuildSingleton();
            }
        }