/// <inheritdoc/>
        public override IGenerationContext Customize(ConstructionContextDefinition constructionContextDefinition)
        {
            if (constructionContextDefinition == null)
            {
                throw new ArgumentNullException(nameof(constructionContextDefinition));
            }

            var wrapper = new GenerationContextWrapper(Inner, constructionContextDefinition);

            return(wrapper);
        }
Пример #2
0
        /// <summary>
        /// Generates the code from the composition <paramref name="container"/>.
        /// </summary>
        /// <param name="container">The <see cref="CompositionContainer"/>.</param>
        /// <returns>The generated code from the composition <paramref name="container"/>.</returns>
        public static (string generatedCode, object[] fieldValues) GenerateCode(this CompositionContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            // First try with simple method names.
            GenerationContext context = new GenerationContextWrapper(
                registrations: container.Registrations,
                compositions: container.Compositions,
                usingSimpleNames: true,
                extraDataType: container.ExtraDataType?.ToCompileName(),
                constructionContext: container.ConstructionContextType?.ToCompileName());

            ProcessCompositions(context);

            // Check if there are any name conflicts.
            if (context.ComposeMethodsNames.Select(c => c.name).Distinct().Count() != context.ComposeMethodsNames.Count)
            {
                context = new GenerationContextWrapper(
                    registrations: container.Registrations,
                    compositions: container.Compositions,
                    usingSimpleNames: false,
                    extraDataType: container.ExtraDataType?.ToCompileName(),
                    constructionContext: container.ConstructionContextType?.ToCompileName());
                ProcessCompositions(context);
            }

            string generatedCode = GenerateCode(context);

            object[] fieldValues =
                context.FieldInitializations.Count == 0
                    ? EmptyFieldValues
                    : context.FieldInitializations.Select(fi => fi.value).ToArray();

            return(generatedCode, fieldValues);
        }