Question

This sort of ties back to a question I had earlier about a regex to search for a method containing a particular string, and someone suggested I use this MS tool called Roslyn but it's not available for VS2010 since 2012 came out.

So I'm writing this small utility to keep a list of every file in my solution that contains a particular method declaration (something like 3k of the 25k files overload this method). Then I simply want to filter that list of files to only ones that contain += inside the body of the method.

static void DirSearch(string dir)
{
    string[] files = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories);

    foreach (var file in files)
    {
        var contents = File.ReadAllText(file);
        if (contents.Contains("void DetachEvents()"))
        {
            //IF DetachEvents CONTAINS += THEN...
                WriteToFile(file);
        }                
     }
}

This method iterates over all the folders and writes the file name to a text file if it contains the key method, but I have no idea how to extract just whatevers in the method body, since it's overloaded all 3K instances of the method are different.

Would the best approach to be get the index of the method name, then the index of each { and } until I encounter the next accessor modifier (signifying I've gotten to the end of DetachEvents)? Then I could just search between indexOfMethod and indexOfEndMethod for +=.

But it sounds really sloppy, I was hoping someone might have a better idea?

Était-ce utile?

La solution 2

I wrote this really sloppy winform that lets the user type in the folder to the code base, the method name, and the flagrant text they're looking for. Then it loops over every file in the directory and calls this method on a string that contains all the text of the file. It returns true if the user-entered flagrant data is present, then the method that calls this adds the file its on to a list. Anyways, here's the major code:

    private bool ContainsFlag(string contents)
    {
        int indexOfMethodDec = contents.IndexOf(_method);
        int indexOfNextPublicMethod = contents.IndexOf("public", indexOfMethodDec);
        if (indexOfNextPublicMethod == -1)
            indexOfNextPublicMethod = int.MaxValue;

        int indexOfNextPrivateMethod = contents.IndexOf("private", indexOfMethodDec);
        if (indexOfNextPrivateMethod == -1)
            indexOfNextPrivateMethod = int.MaxValue;

        int indexOfNextProtectedMethod = contents.IndexOf("protected", indexOfMethodDec);
        if (indexOfNextProtectedMethod == -1)
            indexOfNextProtectedMethod = int.MaxValue;

        int[] indeces = new int[3]{indexOfNextPrivateMethod,
                                   indexOfNextProtectedMethod,
                                   indexOfNextPublicMethod};

        int closestToMethod = indeces.Min();
        if (closestToMethod.Equals(Int32.MaxValue))
            return false; //This should probably do something different.. This condition is true if the method you're reading is the last method in the class, basically

       if (closestToMethod - indexOfMethodDec < 0)
            return false;

        string methodBody = contents.Substring(indexOfMethodDec, closestToMethod - indexOfMethodDec);
        if (methodBody.Contains(_flag))
            return true;
        return false;
    }

Plenty of room for improvement, this is mostly just a proof-of-concept thing that'll get used maybe twice per year internally. But for my purposes it worked. Should be a good starting-point for something more sophisticated if anyone needs it.

Autres conseils

Do you have to do this in code? Is this a one time utility to identify the problem methods? Why not use something like Notepad++ and it's Find in Files capabilities. You can filter your find pretty easily and even apply regex (I think). From there you can copy the results which include the file name (i.e. someclassfile.cs) and get a list from there.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top