CSScriptCompilers.CCSharpParser.IsMethodDeclaration C# (CSharp) Méthode

IsMethodDeclaration() private méthode

private IsMethodDeclaration ( string code ) : bool
code string
Résultat bool
        bool IsMethodDeclaration(string code)
        {
            //RE would work here very well too (for whole text not for line) and it might be introduced in the future.
            //The following RE will return all matches for patern: "word ([word word[,word word]]) {"
            //\s+ \w+ \s* \(  [\s* \w+ \s+ \w+ \s*]* [,\s* \w+ \s+ \w+ \s*]* \)   \s* { 

            int lBracket = code.IndexOf("(");
            if (lBracket != -1)
            {
                int rBracket = code.IndexOf(")", lBracket);
                if (rBracket != -1)
                {
                    if (code.Length - 1 != rBracket) //rBracket must be the last character in the trimmed line (to ignore 'void methods' calls)
                    {
                        string leftOvers = code.Substring(rBracket + 1).Trim();
                        if (!leftOvers.StartsWith("{"))
                            return false;
                    }

                    string[] betweenBracketsContent = code.Substring(lBracket, rBracket - lBracket).Trim("()".ToCharArray()).Split(",".ToCharArray());
                    if (betweenBracketsContent.Length != 0)
                        foreach (string declaration in betweenBracketsContent)
                            if (declaration.Length != 0)
                            {
                                int tokenCount = 0;
                                foreach (string token in declaration.Trim().Split("\t\n\r ".ToCharArray()))
                                    if (token != "" && token != "in" && token != "out" && token != "ref")
                                        tokenCount++;

                                if (tokenCount != 2 && tokenCount != 0)
                                    return false;
                            }

                    return true;
                }
            }
            return false;
        }
        bool IsUsingDirective(string code)