是C#中触发的递归函数

时间:2018-03-21 09:21:53

标签: c# loops recursion

我在函数内部调用了一个函数。但是,我发现Calculate()函数内的函数GetExpression()运行了很多次。似乎运行一些循环或运行递归。我无法弄清楚它是如何运行多次,不知道它是如何循环或递归。我在应用程序日志中发现,当运行Calculate funtions时,GetExpression会多次运行。我错过了一些信息,以了解它是如何循环的?请帮忙。

public DataSet Calculate(int compID, DateTime? fromDate, DateTime? toDate, string indiIDs)
{
    bool allowGetting = true; 
    DataSet results = null; 

    try
    {            
        string unit = null;
        bool isEnabled = IsResultsEnabled(comID);
        if (isCalResultEnabled)
        {
            unit = GetUnit(comID, indicatorIDs, callerAcceptedUnit);
            results = CheckResults(comID, indicatorIDs, unit);
            EnsureUpToDate(comID, results);
            if (calculatedResults.Tables[0].Rows.Count > 0)
            {
                allowGetting = false;
            }
        }

        if (!allowGetting && results != null)
        {
            DataSet ds = GenerateResults(results);
            return ds;
        }
        else
        {                    
            string expression = GetExpression(compID, fromDate, toDate, indiIDs);        

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            ds.Tables.Add(dt);
            dt.Columns.Add("Expression");
            dt.Columns.Add("Result", Type.GetType("System.Decimal"));
            DataRow newRow = dt.NewRow();
            newRow["Expression"] = expression;

            if (string.IsNullOrWhiteSpace(expression))
            {
                newRow["Result"] = DBNull.Value;
            }
            else
            {
                if (expression.EndsWith("+"))
                {
                    expression = expression.Substring(0, expression.Length - 1);
                }

                object result = null;
                try
                {                            
                    result = dt.Compute(expression, "");
                    newRow["Result"] = Convert.ToDecimal(result);
                }
                catch (Exception ex)
                {
                    Logger.Log(string.Format("Error occurs");
                    throw;
                }
            }

            dt.Rows.Add(newRow);
            return ds;
        }
    }
    catch
    {
        throw;
    }
    finally
    {
        if (results != null)
        {
            results.Dispose();
            results = null;
        }
    }
}

0 个答案:

没有答案