Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find all occurrences of comparison with == in visual studio

I made the mistake of using == for comparing IP addresses instead of using the equals() method of the IPAddress class in C#, which will result in the comparison of references instead of values. Since the solution I am currently working on is very large for a one-man project (> 100.000 lines of source code), I am very sure that I still have some of these wrong statements in my code.

Is there any possibility to tell Visual Studio to find all occurrences of == operations on a specific class for me, so that I can find and clean up the bugged comparisons?

with best regards, emi

like image 334
Emiswelt Avatar asked Jan 03 '11 18:01

Emiswelt


People also ask

Where is all occurrences in Visual Studio?

all you need to do is hold Ctrl and Shift and press F or click on Edit -> Find and Replace -> Find In Files. This will open up a dialog box that will let you enter your search criteria. Simply hit the Find All button and you will see a list of results returned to you that match your criteria.

How do I search an entire solution in Visual Studio?

Ctrl + Shift + F – Find in Solution Sometimes you want to search across your entire solution. You can double-click each item in the “Find Results” window to navigate to that instance of the search term you searched for.

How do I search all in Visual Studio?

Visual Studio 17.2 Preview 3 introduces a brand-new All-In-One search experience that merges the existing VS Search (Ctrl + Q) and Go To (Ctrl + T) to allow you to search both your code and Visual Studio features quicker and easier than ever, all in the same place.

How do I search for a word in an entire project in Visual Studio?

Find in Files (Ctrl+Shift+F): A more robust option, Find in Files is better for searching entire projects or solutions. Unlike Quick Find, Find in Files can list search results in the Find Results window and has additional options to specify which file extensions to search.


3 Answers

It's a bit of a hack but you can temporarily add this class to your project:

namespace System.Net
{
    class IPAddress
    {
        [Obsolete]
        public static bool operator ==(IPAddress a, IPAddress b) { return true; }
        [Obsolete]
        public static bool operator !=(IPAddress a, IPAddress b) { return true; }
    }
}

Compile and look for warnings about using obsolete methods:

Warning 'IPAddress.operator ==(IPAddress, IPAddress)' is obsolete

Once you have fixed the code, remove the class definition.

like image 74
Mark Byers Avatar answered Oct 30 '22 12:10

Mark Byers


You could always use a find / replace on "==". You can use the filters to determine what / where you want to search or just use the Entire Solution.

like image 29
Rion Williams Avatar answered Oct 30 '22 12:10

Rion Williams


You might be able to use .NET Reflector or maybe the Visual Studio call hierarchy window to look for calls to the operator== method of the IPAdress class. I don't know if this is possible, just throwing out an idea.

like image 28
Don Kirkby Avatar answered Oct 30 '22 11:10

Don Kirkby