find all occurrences of comparison with == in visu

2020-06-21 05:31发布

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

5条回答
疯言疯语
2楼-- · 2020-06-21 05:43

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.

查看更多
狗以群分
3楼-- · 2020-06-21 05:44

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.

查看更多
Evening l夕情丶
4楼-- · 2020-06-21 05:46

You might subclass IPAddress and override the == operator. This of course depends on how easily you can replace the references. Once you've done that, you could stop there or replace all instances of your == operator with .Equals()

查看更多
贼婆χ
5楼-- · 2020-06-21 05:47

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.

查看更多
甜甜的少女心
6楼-- · 2020-06-21 05:51

If you know the name of the variable representing the IP address over your code, then yes, it is possible with some workaround. Say your variable is called 'ipAddress'. Then do this:

Using wildcards search for:

ipAddress*==

Then loop through the results and make a macro that make the change for you. For example, let's suppose your statement looks like this:

if (ipAddress == anotherIpAddress) {

Then you make a micro as follows:

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

Now you have a macro that change the line for you. All you have to do is to repeatedly press F4 then Ctrl^P. Pressing F4 moves you to the next results in Find in Files (I suppose you will use this), and pressing Ctrl^P executes the macro.

There is a better solution actually using regular expressions but I am not sure whether it works with visual studio. Basically, it groups elements in Find and use them in Replace. So you search for something like "ipAddress == ( < my variable pattern > )" and replace it with "ipAddress.equals(\1)", the one here refers to the first group.

Hope that helps!

查看更多
登录 后发表回答