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.
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
****** 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...
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
"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message
news:m7srevkv9p3hkpp1v...@4ax.com...
> 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