简体   繁体   English

什么时候/为什么要在VB.Net中使用扩展方法?

[英]When/Why would I use an extension method in VB.Net?

I'm new to VB.Net, and I am curious about the use cases for extension methods. 我是VB.Net的新手,并对扩展方法的用例感到好奇。 Specifically, why would I use an extension method when I have inheritance and interfaces at my disposal? 具体来说,当我拥有继承和接口时,为什么还要使用扩展方法? At first glance, extension methods don't seem to me like a very OO practice, and it seems like they would lead to less readable code as opposed to using subclasses and/or interfaces to achieve the same purpose. 乍一看,扩展方法在我看来并不像是一种非常面向对象的实践,并且与使用子类和/或接口来实现相同的目的相反,它们似乎导致可读性较低的代码。 Is there something special about extension methods that I'm missing? 我缺少扩展方法有什么特别之处吗? When do you use them? 您什么时候使用它们?

Before implementing an extension method, people should always try to see if it could fit inside a standard OO class. 在实施扩展方法之前,人们应该始终尝试查看它是否适合标准的OO类。

However, there's 2 situations I can think of where those extension may be very handy: 但是,在两种情况下,我可以想到这些扩展可能非常方便的地方:

1) Extension of primitive type: You don't want to derive a primitive type (like Int or Date) just to add an method to it. 1)基本类型的扩展:您不想要派生一个基本类型(例如Int或Date),而只是想向其添加方法。 I'm not even sure if you're allowed to derive a primitive type but even if you could, it doesn't mean that you should. 我什至不确定是否允许您派生原始类型,但即使可以,也不意味着您应该这样做。

2) Extension of common object: Let's say you want to write some addons functionalities to a very common object; 2)通用对象的扩展:假设您要为一个非常通用的对象编写一些插件功能; let's say add a ToJSON method to a DbDataReader. 假设将ToJSON方法添加到DbDataReader。 You may force users the use your derived Type of DbDataReader (CustomDbDataReader) but they would have to change the code all over the place to use your new class. 您可以强迫用户使用派生的DbDataReader类型(CustomDbDataReader),但他们必须在各处更改代码才能使用新类。 Using an extension would allow them to use your new method on a native DbDataReader. 使用扩展名将使他们能够在本机DbDataReader上使用您的新方法。

You'll also notice that the DbDataReader is an abstract class. 您还将注意到DbDataReader是一个抽象类。 SqlDataReader and OleDbDataReader both derive from the DbDataReader class. SqlDataReader和OleDbDataReader都从DbDataReader类派生。 If you want to write a legit OO function, you would have to write one for SqlDataReader and another one for the OleDbDataReader, even if the function does exactly the same thing. 如果要编写合法的OO函数,则必须为SqlDataReader编写一个,而为OleDbDataReader编写另一个,即使该函数执行的操作完全相同。

You are right that extension methods are not a very important tool in the OO-toolbox. 没错,扩展方法不是OO工具箱中非常重要的工具。 So you might not use them very often to extend classes in your own code. 因此,您可能不会经常使用它们在您自己的代码中扩展类。

As you also write, they might lead to confusion when reading other developers' code because you can only use extension methods that are located in a namespace that you have imported. 如您所写,它们在阅读其他开发人员的代码时可能会引起混乱,因为您只能使用位于已导入名称空间中的扩展方法。

Nevertheless, extension methods are very handy if you want to create helper methods that extend classes that you cannot change. 但是,如果您要创建辅助方法来扩展无法更改的类,则扩展方法非常方便。 That is the main use case extension methods are created for. 这是为其创建主要用例扩展方法。

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

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