lundi 11 mars 2019

c# How do I display the parameters of the current function?

I have a simple Send(message) function which returns the time it took for the function to execute and or ERROR if the time exceeds the specified time, I wanna also return the arguments that were used to run the function. Can I do that?

Something like Send("Goodbye {0}", 0.60d, "World") returns (0.7597, The function was declined, ["Goodbye {0}", "0.60d", "World"])

using System;
using System.Diagnostics;
using static ToolsAndUtilities.Tools;

namespace ConsoleApp20
{
    class Program
    {
        static void Main(string[] args)
        {
            Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send(Send("Hello {0}", 1.00d, "World"))))))))))))))))))))))))))))))))))))))))))))))))))));

            Send("Hello {0}", "World");

            Send("Goodbye {0}", 0.60d, "World");

            //> (0.7597, The function was declined) 

            // What I wanna do:

            //> (0.7597, The function was declined, ["Goodbye {0}", "0.60d", "World"])
        }
    }
}

namespace ToolsAndUtilities
{
    using static ToolsAndUtilities.Utilities;

    public static class Tools
    {
        public static String Nl = Console.Out.NewLine;

        public static Tuple<Double, String> Send(Object Message, params Object[] Args)
        { 

            Stopwatch stopwatch = Stopwatch.StartNew();

            Console.Out.Write($"> {String.Format(Message.ToString(), Args)}{Nl}");

            stopwatch.Stop();

            String functionResult = GetResult(FunctionResult.Passed);

            if (Message is Tuple<Double, String> && stopwatch.Elapsed.TotalMilliseconds >= (Message as Tuple<Double, String>).Item1)
            {
                functionResult = GetResult(FunctionResult.Declined);
            }

            return Tuple.Create<Double, String>(stopwatch.Elapsed.TotalMilliseconds, functionResult);
        }

        public static Tuple<Double, String> Send(Object Message, Double CallSelf, params Object[] Args)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            Console.Out.Write($"> {String.Format(Message.ToString(), Args)}{Nl}");

            stopwatch.Stop();

            String functionResult = GetResult(FunctionResult.Passed);

            if (stopwatch.Elapsed.TotalMilliseconds >= CallSelf)
            {
                functionResult = GetResult(FunctionResult.Declined);
            }

            if (!(CallSelf is default(Double)))
                return Send(Tuple.Create<Double, String>(stopwatch.Elapsed.TotalMilliseconds, functionResult));

            return Tuple.Create<Double, String>(stopwatch.Elapsed.TotalMilliseconds, functionResult);
        }
    }

    public static class Utilities
    {
        public enum FunctionResult
        {
            Passed = 0, Declined = 1
        }

        public static String GetResult(FunctionResult FunctionResult)
        {
            switch (FunctionResult)
            {
                case FunctionResult.Declined:
                    return "The function was declined.";
                case FunctionResult.Passed:
                    return "The function passed successfully.";
                default:
                    return "Result not found.";
            }
        } 
    }
}





Aucun commentaire:

Enregistrer un commentaire