private static Stream CreateXmlCore <TTuple>(ActionFactory <TTuple> factory, Action <XmlWriterSettings> setup) where TTuple : Template <XmlWriter>
        {
            var          options = DelegateUtility.ConfigureAction(setup, OverrideDefaultSettings);
            Stream       output;
            MemoryStream tempOutput = null;

            try
            {
                tempOutput = new MemoryStream();
                using (XmlWriter writer = XmlWriter.Create(tempOutput, options))
                {
                    factory.GenericArguments.Arg1 = writer;
                    factory.ExecuteMethod();
                    writer.Flush();
                }
                tempOutput.Flush();
                tempOutput.Position = 0;
                output     = tempOutput;
                tempOutput = null;
            }
            catch (Exception ex)
            {
                List <object> parameters = new List <object>();
                parameters.AddRange(factory.GenericArguments.ToArray());
                parameters.Add(options);
                throw ExceptionUtility.Refine(new InvalidOperationException("There is an error in the XML document.", ex), factory.DelegateInfo, parameters.ToArray()).Unwrap();
            }
            finally
            {
                tempOutput?.Dispose();
            }
            return(output);
        }
        private static TimeMeasureProfiler WithActionCore <TTuple>(ActionFactory <TTuple> factory, Action <TimeMeasureOptions> setup) where TTuple : Template
        {
            var options    = setup.ConfigureOptions();
            var descriptor = options.MethodDescriptor?.Invoke() ?? new MethodDescriptor(factory.DelegateInfo);
            var profiler   = new TimeMeasureProfiler()
            {
                Member = descriptor.ToString(),
                Data   = descriptor.MergeParameters(options.RuntimeParameters ?? factory.GenericArguments.ToArray())
            };

            PerformTimeMeasuring(profiler, options, p => factory.ExecuteMethod());
            return(profiler);
        }
        private static Stream CreateStreamCore <TTuple>(ActionFactory <TTuple> factory, Action <StreamWriterOptions> setup = null) where TTuple : Template <StreamWriter>
        {
            var          options = setup.ConfigureOptions();
            Stream       output;
            MemoryStream tempOutput = null;

            try
            {
                tempOutput = new MemoryStream(options.BufferSize);
                StreamWriter writer = new InternalStreamWriter(tempOutput, options);
                {
                    factory.GenericArguments.Arg1 = writer;
                    factory.ExecuteMethod();
                    writer.Flush();
                }
                tempOutput.Flush();
                tempOutput.Position = 0;
                output     = tempOutput;
                tempOutput = null;
            }
            catch (Exception ex)
            {
                List <object> parameters = new List <object>();
                parameters.AddRange(factory.GenericArguments.ToArray());
                parameters.Add(options);
                throw ExceptionUtility.Refine(new InvalidOperationException("There is an error in the Stream being written.", ex), factory.DelegateInfo, parameters.ToArray()).Unwrap();
            }
            finally
            {
                if (tempOutput != null)
                {
                    tempOutput.Dispose();
                }
            }
            return(output);
        }