简体   繁体   English

根据typeof搜索和替换

[英]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. 我希望这里的人知道如何使用Visual Studio 2013按类型查找文本并替换它们。 For example, I have a struct that uses operator overloading to multiply. 例如,我有一个使用运算符重载进行乘法运算的结构。 So throughout my program I have statements/expressions such as struct * struct. 因此,在我的程序中,我都有诸如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? 有没有一种方法可以搜索Vector3乘以Vector3的类型,而不是例如float乘以float的类型,然后用其他替换它的位置?

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. 我大约有8000多个案例,因此手动进行会很痛苦。

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. 我在Resharper的Refactor中尝试了搜索模式,但是该加载项弄乱了我的整个代码。 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. 我可能用错了,如果有人知道如何在Refactor中使用搜索模式完成上述操作,请也告诉我。

Thanks in advance! 提前致谢!

One possibility: break the operator overload of * on Vector3, then rebuild. 一种可能性:打破Vector3上*的运算符重载,然后重新构建。 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. 您应该能够在ReSharper中使用“ 结构搜索和替换”模式 Something like: 就像是:

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

Where $id$ is an identifier placeholder, that you can pretty much ignore. 其中$id$是标识符占位符,您几乎可以忽略它。 $exp1$ and $exp2$ would be expression placeholders that have a type of your Vector3 (but with a fully qualified type name). $exp1$$exp2$将是具有Vector3类型(但具有完全限定的类型名称)的表达式占位符。 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). 然后ReSharper会用此代码替换与find模式匹配的代码,并代回原始标识符以及原始表达式(这些表达式可以是表达式,例如方法调用的返回值,而不仅仅是局部变量) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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