如何显示当前功能的参数?

时间:2019-03-11 10:53:27

标签: c# reflection

我有一个简单的Send(message)函数,该函数返回执行该函数所花费的时间,如果返回的时间超过指定的时间,则返回ERROR,我还想返回用于运行该函数的参数。我可以这样做吗?

类似Send("Goodbye {0}", 0.60d, "World")的事物会返回(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.";
            }
        } 
    }
}

0 个答案:

没有答案