从对象实例访问共享函数时会发生什么

发布于 2024-10-09 15:40:37 字数 865 浏览 4 评论 0原文

这个问题的背景是我正在尝试调试性能问题(除了我已经知道并且可以找到的明显问题之外)。

我继承了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘年祭陌 2024-10-16 15:40:37

来自 VB.NET规格(我的重点)。

共享方法不能在
类型的特定实例
并且可能是
直接从类型调用而不是
而不是通过一个特定的实例
一种类型。但是,使用它是有效的
限定共享的实例
方法。

因此,不,您是否从实例调用它并不重要。

From the VB.NET spec (my emphasis).

A shared method does not operate on a
specific instance of a type
and may be
invoked directly from a type rather
than through a particular instance of
a type. It is valid, however, to use
an instance to qualify a shared
method.

Therefore, no, it does not matter whether you are calling it from an instance or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文