问题描述
我从来不喜欢含蓄运营商(preFER扩展方法),因为它是很难看到在视觉上时投/转换发生在code。
I never liked implicit operators (prefer extension methods) because it is hard to see visually when that cast/conversion happens in the code.
试想一下,如果你有类似的例子如下:
Imagine if you have example like below:
public static implicit operator Deal(string dealAsXml)
{
//convert the xml into Deal object
}
以上隐含的运营商可以帮助你投/转换协议的XML格式成为交易对象。
Above implicit operator helps you to cast/convert deal in Xml format into Deal Object.
通常,当你右键点击一个方法,你可以使用查找用法(或按Alt + F7)就可以了,这是相当有帮助的,有什么相似的隐式操作?
Usually when you right click on a method, you can use "Find Usages" (or Alt+F7) on it, which is quite helpful, is there anything similar for implicit operators ?
我认为这是另一个原因在哪里可以使用扩展方法。
I think that's another reason to use the Extensions methods where possible.
推荐答案
也许像ReSharper的可以做到这一点,但我不知道。当我需要找到用法,我这样做是可怜的人的生活方式和消除隐性运营商,重新编译和查找错误。 >>
Maybe something like Resharper can do it, but I'm not sure. When I need to find usages, I do it the poor-man's way and remove the implicit operator, recompile and find the errors. >.>
我想理论上编译器可能然后错过的情况下,如果它可以使用不同的隐式操作者(或切换到的方法的对象的类型的过载),但它往往工作为我的用法。我敢肯定有一个更好的解决方案,但它至今为我工作。
I suppose theoretically the compiler might then miss a case if it can use a different implicit operator (or switch to an "object" type overload of a method), but it tends to work for my usages. I'm sure there's a better solution, but it's worked for me so far.
编辑:只是有一个想法,并对其进行了测试。标记您的暗示运营商为 [作废]
实际上会导致编译器警告,无论你使用它!我想这会捕捉那些角落情况下,也有,你会怀念完全删除了隐含的经营者其他有效重载。
Just had a thought and tested it. Marking your implicit operator as [Obsolete]
will actually result in a compiler warning wherever you use it! I suppose this will catch those corner cases where there are other valid overloads that you'd miss having removed the implicit operator altogether.
这篇关于如何获取和QUOT;查找用法"隐式运营方式工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!