从对象实例访问共享函数时会发生什么
这个问题的背景是我正在尝试调试性能问题(除了我已经知道并且可以找到的明显问题之外)。
我继承了 ASP.NET 应用程序的代码库 (VB.NET)。这是一个早在 .NET 1.1 时代就开发的应用程序,对于许多开发人员来说是第一个 .NET 应用程序。
在此代码库中,有一个名为 DatabaseUtility 的类,其中包含多个共享公共方法以及用于数据库 (SQL Server) 的 CRUD 操作的非共享公共函数和子函数。
在我的“BL”中,一种常见的方法是创建 DatabaseUtility 的实例,该实例本质上确定连接字符串应该是什么并打开连接,并为开发人员提供其中包含的其他方法的句柄。
Dim utility as New DatabaseUtility()
一旦有了这些,我就开始创建参数,并将其传递给 DatabaseUtility 中的方法之一(例如 GetDataSet)。我的 DatabaseUtility 中有一个名为 CreateParameter 的共享方法,它基本上就是这样做的。它创建一个 SqlParameter 对象,以便我可以将其添加到Parameters 集合中。
现在,代码库中充斥着很多这样的内容:
utility.CreateParameter(...)
但是,因为 CreateParameter 是一个共享方法,所以我不确定幕后发生了什么。我知道,因为它是一个 Shared 成员,所以当我像这样调用它时,不会创建 DatabaseUtility 的实例:
DatabaseUtility.CreateParameter(...)
但是,因为我是从实例(实用程序)调用它,所以这会改变行为吗?
The context of this question is that I am trying to debug performance issues (apart from the obvious ones I already know about and can find).
I inherited a code base (VB.NET) for an ASP.NET app. This is an app that was developed way back in .NET 1.1 days, and was the first .NET app for a lot of the developers who worked on it.
In this code base is a class called DatabaseUtility that contains several Shared Public methods as well as non-Shared Public Functions and Subs for CRUD operations to the database (SQL Server).
It is common in my "BL" that a method creates an instance of the DatabaseUtility which essentially figures out what the connection string should be and opens a connection, as well as giving the developer a handle to the other methods contained within it.
Dim utility as New DatabaseUtility()
Once I have that, I start to create parameters that I am going to pass to one of the methods in DatabaseUtility (like GetDataSet). There is a Shared method in my DatabaseUtility called CreateParameter which does essentially that. It creates a SqlParameter object so I can add it to a Parameters collection.
Now, the code base is littered with a lot of this:
utility.CreateParameter(...)
However, because CreateParameter is a Shared method, I am not sure what is going on behind the scenes. I know because it is a Shared member that an instance of the DatabaseUtility is not created when I call it like this:
DatabaseUtility.CreateParameter(...)
However, because I am calling it from an instance (utility), does that change the behavior at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 VB.NET规格(我的重点)。
因此,不,您是否从实例调用它并不重要。
From the VB.NET spec (my emphasis).
Therefore, no, it does not matter whether you are calling it from an instance or not.