Find all occurrences of comparison with == in visual studio

I made the mistake of using == to compare IP addresses instead of using the equals () method of the IPAddress class in C #, which will result in a comparison of links instead of values. Since the solution I'm working on right now is very large for a one-person project (> 100,000 lines of source code), I am pretty sure that I still have some of these incorrect statements in my code.

Is there any way to tell Visual Studio to find all occurrences of == operations for a particular class for me so that I can find and clear the listening comparisons?

best regards, emi

+5
source share
5 answers

This is 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 find warnings about using legacy methods:

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

After fixing the code, remove the class definition.

+18
source

You can always use find / replace on "==". You can use filters to determine what / where you want to search or just use all the solutions.

+3
source

, .NET Reflector , , Visual Studio, operator == IPAdress. , , .

+1

, IP- , , . , "ipAddress". :

:

ipAddress*==

, . , , :

if (ipAddress == anotherIpAddress) {

:

Start Recording
Press Home              # This will go to the beginning of the line
Ctrl+Right Three Times  # This will keep the cursor on the beginning of anotherIpAddress
Backspace               # This will remove the space
.equals(                # This will write .equals(
Del Three Times         # This will delete the == and the space after it
Ctrl+Right              # This will keep you at the closing bracket ).
)                       # This will write another closing bracket for the equals functions.
Stop Recording

, . , , - F4, Ctrl ^ P. F4 ( , ), Ctrl ^ P .

, , . , Find Replace. , - "ipAddress == (< my variable pattern > )" "ipAddress.equals(\ 1)", , .

, !

0

IPAddress ==. , , , . , == .Equals()

0

All Articles