Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

(HttpWebRequest) WebRequest.Create - cast up?!

0 views
Skip to first unread message

Kevin C

unread,
Jun 16, 2003, 10:40:07 AM6/16/03
to
How come the following code is allowed to work:

HttpWebRequest req = (HttpWebRequest)
WebRequest.Create(http://test/test.aspx);

Isn't this attempting to "cast up"? A HttpWebRequest is a WebRequest but a
WebRequest is not necessarily an HttpWebRequest.

I would like to subclass HTTPRequest but don't have a clean way of creating
my new subclassed object because everything is created out of the WebRequest
factory.


Jonathan Schafer

unread,
Jun 16, 2003, 11:08:16 AM6/16/03
to
Because WebRequest actually creates a HttpWebRequest object using that
static method (it also can create a FileWebRequest object.

My guess is you would have to derive a class from WebRequest, create
your own static method which would create your derived HttpWebRequest
instead of the standard HttpWebRequest object.

Can you give more detail explaining why you want to create your own
derived class? There's not much you can't already do (if anything),
with HttpWebRequest.

Jonathan Schafer

Kevin C

unread,
Jun 16, 2003, 11:39:20 AM6/16/03
to
I have actually nixed the subclassing part :) .... but I'm still unclear how
that static function "knows" to create an HttpWebRequest, opposed to a
NetWebRequest.

****** I know it is not doing this, but it appears as if its doing the
following:

public class Animal {
}

public class Dog : Animal {
}

Dog d = (Dog) new Animal(); //cannot happen - "casting up"

"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message
news:e2nrev4uauvtifd94...@4ax.com...

Jonathan Schafer

unread,
Jun 16, 2003, 12:36:17 PM6/16/03
to
No, what it is doing is this...


public class Animal
{
static Animal Create(string strType)
{
if (strType == "Dog")
return new Dog();
else
return new Cat();
}
}

public class Dog : Animal
{
}

public class Cat : Animal
{
}

In the case of WebRequest, it analyzes the contents of either the
string or the Uril (dependent on which version you call), determines
if you need an HttpWebRequest or a FileWebRequest, creates the derived
type, but returns the reference to you as a base type. The base type
still knows it is a Cat or Dog, regardless of whether it is returned
as an Animal base type.

Jonathan Schafer

Kevin C

unread,
Jun 16, 2003, 1:10:28 PM6/16/03
to
Ahh, I see ... I see the 2 constructors but didn't realize it parse the
string/inspected the Uri to determine type. Thanks for the explanation.


"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message

news:m7srevkv9p3hkpp1v...@4ax.com...

Joerg Jooss

unread,
Jun 17, 2003, 8:53:54 AM6/17/03
to
C" <KC> spoke:

> How come the following code is allowed to work:
>
> HttpWebRequest req = (HttpWebRequest)
> WebRequest.Create(http://test/test.aspx);
>
> Isn't this attempting to "cast up"? A HttpWebRequest is a
> WebRequest but a WebRequest is not necessarily an HttpWebRequest.

This is a down cast -- HttpWebRequest is "below" WebRequest in the
System.Net hierarchy.

Cheers,
--
Joerg Jooss
joerg...@gmx.net

0 new messages