微软是如何做到这一点的?(关于他们的.NET HttpServerUtility类的OO问题)

微软是如何做到这一点的?(关于他们的.NET HttpServerUtility类的OO问题),.net,oop,.net,Oop,HttpServerUtility包含一个名为UrlEncode的公共函数。它不是一个共享功能。HttpServerUtility没有任何公共构造函数 这样做失败: Dim encodeMe As String = "a string to be encoded!" HttpServerUtility.UrlEncode(encodeMe) 'Bombs out 这是可行的,微软就是这么说的: Dim instance As HttpServerUtility Dim encodeMe As

HttpServerUtility包含一个名为UrlEncode的公共函数。它不是一个共享功能。HttpServerUtility没有任何公共构造函数

这样做失败:

Dim encodeMe As String = "a string to be encoded!"
HttpServerUtility.UrlEncode(encodeMe) 'Bombs out
这是可行的,微软就是这么说的:

Dim instance As HttpServerUtility
Dim encodeMe As String = "a string to be encoded!"

instance.UrlEncode(encodeMe ) 'Works!
他们是如何做到这一点的?您不能使用构造函数实例化它的实例,但不能仅通过引用HttpServerUtility.UrlEncode来访问UrlEncode


编辑:虽然我非常喜欢大家参与一场OO大辩论,但我相信问题在于MSDN文档有缺陷。行“Dim instance As HttpServerUtility”应为“Dim instance As HttpServerUtility=Context.Server”。我包含的代码(来自MSDN文档)实际上不起作用,而是引发了一个空引用异常—正如您所期望的那样。谢谢你,杰森

它有一个静态私有构造函数,允许使用它。查看以下链接:

如果你想看看他们做了什么,下载reflector并打开它


编辑:找出答案,让人们高兴。虽然静态构造函数是私有的,它允许像问题中那样编写代码,但您仍然需要一个实例才能使其正确运行

你确定这有效吗

Dim instance As HttpServerUtility
Dim encodeMe As String = "a string to be encoded!"
instance.UrlEncode(encodeMe) 'Works!
这将在运行时为您提供一个
NullReferenceException
(并且编译器将向您提供一个警告,提示未将
instance
分配给它)。说真的,微软在这里什么都没做。上面的代码是灾难性的错误,将在运行时死亡

你不能这么做

Dim encodeMe As String = "a string to be encoded!"
HttpServerUtility.UrlEncode(encodeMe) 'Bombs out
因为
HttpServerUtility
中未将
UrlEncode
定义为
Shared
方法

您需要
HttpServerUtility
的非空实例。使用
HttpServerUtility
的正确方法如下:

Dim instance As HttpServerUtility = HttpContext.Server
Dim s As String = "Hello, World!"
Dim result As String = instance.UrlEncode(s)
另一种选择是只使用
HttpUtility
,其中有一个
Shared
方法
HttpUtility.UrlEncode

Dim s As String = "Hello, World!"
Dim result As String = HttpUtility.UrlEncode(s)

使用HttpUtility.UrlEncode()而不是HttpServerUtility.UrlEncode()。HttpServerUtility上的版本是实例方法,而不是共享/静态方法。这与静态构造函数无关(第一次调用类中的静态方法时将调用静态构造函数)

首先,您给出的代码示例都不起作用

第一个示例将不起作用,因为UrlEncode是一个实例方法,因此您不能在类型上调用它,即HttpServerUtility.UrlEncode(encodeMe)

第二个示例将不起作用,因为尚未分配变量

这与静态构造函数无关,并且发布的答案本身具有误导性


HttpServerUtility类型设计为仅由System.Web程序集在内部初始化。您不能创建自己的实例。您可以使用HttpContext.Server(返回HttpServerUtility的实例)在web应用程序中访问它的实例。

用什么?“对象引用未设置为对象的实例”?OOP 101:静态/类方法!=实例方法你在哪里找到了你的第二个代码,微软说它在哪里工作?还有:你真的测试过吗?它不起作用。虽然文档可能会让人困惑,但我不确定我是否会说它是错的。“Visual Basic(用法)”框旨在让您了解如何声明正确类型的变量以及如何对其调用方法。它不显示如何获取实例。此信息在所有文档中都有复制,例如,应该用于测试的代码是示例代码框中的代码。他们甚至在这些框中添加了“复制代码”链接,以避免需要在示例中选择代码。您不会将我们链接到错误的MSDN文档吗?这有点粗鲁。是的,但关键是你不必实际创建实例来让它工作,即使它是实例成员,而不是共享成员。看在Donald Knuth的份上,不。这个答案是错误的,不应该有任何投票权。仅仅因为存在静态构造函数,就不允许在未分配实例上调用方法,也不允许运行时异常。这条线索的混乱程度令人震惊。杰森说的。可以对空引用调用此方法。它通常会以NullReferenceException终止。Jason的+1@Joshua-变量声明绝对不会创建type@Joshua-您的编辑也不正确。静态构造函数不允许像问题中那样编写代码。问题中代码的两个版本都不相同work@divo:那么它在MSDN上是错误的(不是第一次)。它将在运行时崩溃。HttpUtility.UrlEncode(encodeMe)将为此节省大量时间和痛苦debate@Jason:我查看了MSDN对该方法的描述,没有发现任何错误-尽管文档不好。@Jason我相信你是对的-文档不好,应该读取HttpServerUtility instance=HttpContext.Server;我只是看了一下文件。我认为混淆源于标记为“VisualBasic(用法)”()的代码段。这就是VB.NET的所有文档是如何用MSDN编写的。它告诉您,如果您有一个类型为
HttpServerUtility
的名为
instance
的变量和一个类型为string的名为
s
的变量,那么您可以通过
instance.UrlEncode>调用方法
HttpServerUtility.UrlEncode
。有关这类文档的另一个示例,请查看
StreamReader.Read
()。