简体   繁体   English

扩展方法是否应该有前缀?

[英]Should extension methods have a prefix?

From what I have read about extension methods, you will run into problems if the base class decides to add a method with the same name as your extension. 根据我读到的有关扩展方法的内容,如果基类决定添加与扩展名相同的方法,则会遇到问题。 For a specific class it is generally not to hard to pick a name to avoid clashes, however extension methods can be added to interfaces which adds infinitely more potential for conflicts. 对于特定的类,通常不难选择名称以避免冲突,但是可以向接口添加扩展方法,这会增加冲突的无限可能性。

In Objective-C (with their version, categories), this problem is avoided by adding a prefix before each method. 在Objective-C(及其版本,类别)中,通​​过在每个方法之前添加前缀来避免此问题。 I know we can define extension methods in namespaces so that we can control whether they are imported or not, but this only solves the problem of clashes between extension methods, rather than clashes between an extension method and the base class. 我知道我们可以在命名空间中定义扩展方法,以便我们可以控制它们是否被导入,但这只能解决扩展方法之间的冲突问题,而不是扩展方法和基类之间的冲突。

Update: 更新:

Nobody, actually mentioned this, but extension methods aren't virtual . 实际上没有人提到这一点,但扩展方法不是虚拟的 That means if you can i.myExtension() on an interface i, then it will always call the interface classes method, so the subclass method (which could have different intent) won't be called. 这意味着如果你可以在接口i上使用i.myExtension(),那么它将始终调用接口类方法,因此不会调用子类方法(可能具有不同的意图)。 So, overall, using extension methods is quite safe. 因此,总的来说,使用扩展方法是非常安全的。

The use of a prefix will make the code ugly, which removes some of the value of an extension method. 使用前缀会使代码变得丑陋,从而消除了扩展方法的一些价值。

I recommend instead, being careful to not create a large number of extension methods and to use method names that make sense. 我建议相反,小心不要创建大量的扩展方法并使用有意义的方法名称。 This way, in the case of a conflict, it's more likely that, for instance, the extension Where method and the conflicting Where method will have similar semantics. 这样,在冲突的情况下,更有可能的是,例如,扩展Where方法和冲突的Where方法将具有类似的语义。

The common convention is to have them in their own separate namespace (usually in the form of <something>.Extensions ) which you can then use to decide whether or not you use them in any particular piece of code which contains types the extensions normally operate on. 常见的约定是将它们放在各自独立的命名空间中(通常以<something>.Extensions的形式),然后您可以使用它来决定是否在包含扩展通常运行的类型的任何特定代码段中使用它们上。

Furthermore, if you do have a conflict, remember that extension methods are still static methods , so you can always call them explicitly in the case of a name conflict. 此外,如果确实存在冲突,请记住扩展方法仍然是静态方法 ,因此在名称冲突的情况下,您始终可以显式调用它们。

However, if you find that you are frequently running into name conflicts, you might want to reconsider the names you are choosing so that they don't interfere with names that already exist on the type. 但是,如果您发现经常遇到名称冲突,则可能需要重新考虑所选的名称,以便它们不会干扰该类型上已存在的名称。

To summarize, no, you should not prefix extension method names in C#, as they only serve to obfuscate your code and make it harder to maintain. 总而言之, 不,您不应该在C#中添加扩展方法名称的前缀,因为它们只会混淆您的代码并使其难以维护。

If the base class implements a method with the same name as your extension, I'd guess you'd have one of these two scenarios: 如果基类实现了一个与扩展名相同的方法,我猜你会有以下两种情况之一:

  1. The base class implemented the same functionality you were adding yourself with an extension method. 基类实现了您使用扩展方法添加的相同功能。 You no longer need the extension method. 您不再需要扩展方法。 Delete it and use the method on the base class instead. 删除它并改为使用基类上的方法。
  2. The base class implemented something different from what you wanted but gave it the same name. 基类实现了与您想要的不同的东西,但赋予它相同的名称。 Either you or they are using the wrong name. 你或他们使用错误的名字。 Probably you. 可能是你。 Rename your extension method to something that describes what the function actually does. 将扩展方法重命名为描述函数实际执行操作的内容。

I'm inclined to say no they shouldn't unless you have reason good to suspect that the method signature you are using is going to be implemented. 除非你有理由怀疑你正在使用的方法签名将被实现,否则我倾向于说不。 The reason i think this is... 我认为这是......的原因

  • It's is quite unlikely that your method signature will be duplicated in most cases. 在大多数情况下,您的方法签名不太可能重复。
  • If it does, it should always be detected in unit tests. 如果是,则应始终在单元测试中检测到。
  • Should be trivial to fix. 应该是微不足道的修复。

I normally opt for relatively verbose method names when implementing extension methods anyway to avoid confusion. 我通常在实现扩展方法时选择相对冗长的方法名称以避免混淆。

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

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