How DID Microsoft do this? (an OO question about their .NET HttpServerUtility class)
HttpServerUtility conta开发者_如何学Cins a public function called UrlEncode. It is not a shared function. HttpServerUtility does not have any public constructors.
Doing this fails:
Dim encodeMe As String = "a string to be encoded!"
HttpServerUtility.UrlEncode(encodeMe) 'Bombs out
This works, and is how Microsoft says to do it:
Dim instance As HttpServerUtility
Dim encodeMe As String = "a string to be encoded!"
instance.UrlEncode(encodeMe ) 'Works!
How did they accomplish this? You can't instantiate an instance of it using a constructor, yet you can't access UrlEncode by just referencing HttpServerUtility.UrlEncode.
EDIT: While I thoroughly enjoyed everyone getting into a big OO debate, I believe the problem is faulty MSDN documentation. The line "Dim instance As HttpServerUtility" should read "Dim instance As HttpServerUtility = Context.Server" The code which I included (which is from the MSDN documentation) does not actually work, and instead throws a null reference exception - just as you'd expect. Thank you, Jason!
Are you sure this works?
Dim instance As HttpServerUtility
Dim encodeMe As String = "a string to be encoded!"
instance.UrlEncode(encodeMe) 'Works!
This will give you a NullReferenceException
at runtime (and the compiler will give you a warning that instance
is not being assigned to). Seriously, Microsoft didn't do anything here. The above code is disastrously wrong and will die at runtime.
And you can't do this
Dim encodeMe As String = "a string to be encoded!"
HttpServerUtility.UrlEncode(encodeMe) 'Bombs out
because UrlEncode
is not defined as a Shared
method in HttpServerUtility
.
You need a non-null instance of HttpServerUtility
. The right way to use HttpServerUtility
is like this:
Dim instance As HttpServerUtility = HttpContext.Server
Dim s As String = "Hello, World!"
Dim result As String = instance.UrlEncode(s)
Another option is to just use HttpUtility
for which there is a Shared
method HttpUtility.UrlEncode
:
Dim s As String = "Hello, World!"
Dim result As String = HttpUtility.UrlEncode(s)
First of all, neither of the code examples you have given will work.
The first example will not work because UrlEncode is an instance method, therefore you cannot call it on the type, i.e. HttpServerUtility.UrlEncode(encodeMe).
The second example will not work because the variable has not been assigned.
This has nothing to do with static constructors and the answers posted stating as such are misleading.
The HttpServerUtility type is designed to be initialised only internally by the System.Web assembly. You cannot create your own instances of it. You can access an instance of it in a web application by using HttpContext.Server (which returns an instance of an HttpServerUtility).
Use HttpUtility.UrlEncode() instead of HttpServerUtility.UrlEncode(). The version on HttpServerUtility is an instance method and not a shared/static method. This has nothing to do with a static constructor (a static constructor would be called the first time a static method from the class is called)
It has a static private constructor which allows it to be used. Check out the following link: Static Constructors
If you want to see what they did download reflector and open it up.
EDIT: To flush out the answer to make people happy. While the static constructor which is private allows the code to be written like it is in the question you do still need an instance for it to function correctly.
精彩评论