Home > Net >  Search and replace but only for a specific type
Search and replace but only for a specific type

Time:10-01

I have written an extension method for a class that I do not own and neither have the source code for. This method is supposed to be used in place of an existing method of the class. Let's say that I want to replace the Something property with my SomethingExt extension method. A lot of instances of this class are scattered around in my code but the problem is that I can't simply do a search and replace for .Something because that would also replace everything on totally unrelated variables that just happen to have a property with the same name. I can easily use "Find all references" on the property but I see no way to do a search and replace for just the lines it is showing me. Rename doesn't work because it can't rename "elements defined in metadata".

Is there any way I can somehow limit Search And Replace to just a specific type? I mean, I can do Ctrl A in the references window but that does not have any effect on the Search And Replace window.

VBNet on Visual Studio 2022.

CodePudding user response:

I don't think there's anything built in so you might have to get creative. One option might to define a class with the same name and members as the one you want to extend. Any references to that other type in your code will then refer to your class preferentially. You can then rename that one property and have VS update all the usages. You can then delete your new class and everything will go back to using the original class, but that property name will be invalid. You can then easily find all those instances.

You'll have to use regular expressions when you do a F&R because you'll have to convert a property setting to a method call and the value won't be the same every time, i.e. .Something = someValue will have to become .SomethingExt(someValue).

EDIT:

In case this might help someone else, it occurs to me that you probably don't even need all the other members on your own type. Just that one property would probably be enough.

  • Related