简体   繁体   中英

Search and Replace based on typeof

I am hoping someone here knows how to use visual studio 2013 to find texts by type and replace them. For example, I have a struct that uses operator overloading to multiply. So throughout my program I have statements/expressions such as struct * struct.

I would like to change all of that to use a method; For example, I would like to change the following:

Vectro3 a;
Vector3 b;

Vector3 c = a * b;

to:

Vector3 a;
Vector3 b;

Vector3 c = Vector3.Mul(a, b);

Is there a way I can search for where type Vector3 multiples Vector3 only and not where for example type float multiples float and then replace it with something else?

The reason I need to change it to a method is because I am going to be then using a program to inline the calculations instead of using operators or methods. I have about over 8000 cases so doing it manually is going to be painful.

I am really hoping someone here knows a technique or a program that could do it. I tried search patterns in Refactor by Resharper, but that add-in messes my whole code. I might be using it wrong, if any one knows how to do the above with search pattern in Refactor please let me know as well.

Thanks in advance!

One possibility: break the operator overload of * on Vector3, then rebuild. This will give you errors wherever you are using the overloaded * . Fix those. It's not as nice as fixing them all at once, but at least it finds them all quickly.

Another possibility. When I drop down a class in the solution explorer, I can drop down to a point where I get a list of its methods. Then I can I can right-click on a method and choose "Is Called By" or "Is Used By". Again, this won't do a Replace for you, but it should help locate all the uses, which is still a help.

You should be able to use a Structural Search and Replace pattern in ReSharper. Something like:

Vector3 $id$ = $exp1$ * $exp2$

Where $id$ is an identifier placeholder, that you can pretty much ignore. $exp1$ and $exp2$ would be expression placeholders that have a type of your Vector3 (but with a fully qualified type name). This should be enough to find all instances of multiplication.

You could then create a replace pattern:

Vector3 $id$ = Vector3.Mul($exp1$, $exp2$)

and ReSharper will replace the code that matched the find pattern with this, substituting the original identifier back in, as well as the original expressions (and these can be expressions, such as the return value of a method call, and not just local variables).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM