C#:发现扩展方法
您推荐使用哪些工具或技术来发现代码中的 C# 扩展方法?它们位于正确的命名空间中,但可能位于解决方案中的任何文件中。
具体来说:
- 如何在代码窗口中找到当前类型的所有扩展方法(并导航到它们)?
我确实有 Resharper (v4),所以如果它有我不知道的机制 - 请分享!
What tools or techniques do you recommend for discovering C# extension methods in code? They're in the right namespace, but may be in any file in the solution.
Specifically:
- How can I find all extension methods (and navigate to them) on the current type in the code window?
I do have Resharper (v4), so if that has a mechanism I'm not aware of - please share!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您有源代码,则可以使用正则表达式搜索
此类型标识符
。考虑到它必须是函数的第一个参数,这样的事情应该可以解决问题:至少通过这种方式,您可以发现扩展方法的定义位置并添加该名称空间,然后依赖于智能感知。刚刚在一个不平凡的项目上运行了这个,到处都有很多扩展方法,而且效果很好。唯一的误报是这样的:
我们可以通过在搜索中添加
static
来修复它,因为扩展方法必须是静态的:这对于这样的情况不起作用:
但这就是正则表达式的限制。我确信某些人可以告诉你所有关于使用正则表达式解析语言的痛苦。
现在,我认为 IDE 缺少的是识别非导入命名空间中的扩展方法的能力。与知道类名时类似,如果您输入类名,IDE 会提示您显式使用它或导入命名空间。毕竟,这就是我导入所有名称空间的方式,并且经常发现自己尝试对扩展方法执行相同的操作。
If you have the source then you can search for
this Type identifier
using regular expressions. Considering the fact that it has to be the first parameter to the function something like this should do the trick:At least this way you can discover where the extensions methods are defined and add that namespace, then rely on intellisense. Just ran this on a non-trivial project with lots of extensions methods everywhere and it worked a treat. The only false positive was something like this:
Which we can fix by adding
static
to our search since the extension methods have to be static:This won't work for cases like this:
But that's kind of the limitation of regular expressions. I am sure certain somebodies can tell you all about the pain of using regular expressions to parse a language.
Now, what I think is missing from the IDE is the ability to recognise the extension methods that are in a non-imported namespace. Similar to when you know the classname, if you type it up, the IDE will give you a hint to either use it explicitly or import the namespace. After all, that's how I import all my namespaces and frequently find myself trying to do the same to extension methods.
这技术含量很低,但是用 Ctrl-F 搜索“this:b+MyClassName”怎么样?
This is pretty low-tech, but how about Ctrl-F searching for "this:b+MyClassName" ?
如果你使用的是VS,我猜你是intellisense,它将为你显示给定对象的所有可用扩展方法(用添加到常用实例方法图标的蓝色东西标记)。尽管对象类型相同(基于扩展方法的查找方式),该列表可能因文件而异(称为 aMethod 的方法可能在两个不同文件中表示两种不同的事物)
If you are using VS which I guess you are intellisense will show all the avialable extensionmethod for a given object for you (marked with a blue thingy added to the usual instance method icon). That list might differ from file to file (a mthod called aMethod might mean two different things in two different files) eventhough the object type is the same (which is based on the way extension methods are found)
如果你有resharper,只需按住ctrl键并单击该方法即可。
If you've got resharper, just hold down the ctrl key and click on the method.
如果您已在 Visual Studio 中安装了 ILSpy 扩展(我使用的是 2022),那么您可以:
If you have installed the ILSpy extension in Visual Studio (I am using 2022) then you can: