By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 1,964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

generic type and cast

P: n/a
delegate void SomeDelegate<T>() where T : BaseClass;

my_event(object sender, myEventArgs e)
{
//e.Tag object is SomeDelegate<DerivedClass>

SomeDelegate<BaseClass> del = (SomeDelegate<BaseClass>)e.Tag; // *** unable
to cast DerivedClass in BaseClass

}

How I cast (if possible) derived generic type to base generic type?
Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Apr 28 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Tommaso Caldarola" <il********@lcimitero.it> a écrit dans le message de
news: 44**********@x-privat.org...

| delegate void SomeDelegate<T>() where T : BaseClass;
|
| my_event(object sender, myEventArgs e)
| {
| //e.Tag object is SomeDelegate<DerivedClass>
|
| SomeDelegate<BaseClass> del = (SomeDelegate<BaseClass>)e.Tag; // ***
unable
| to cast DerivedClass in BaseClass
|
| }
|
| How I cast (if possible) derived generic type to base generic type?

Sorry, this is not possible, you are trying to cast from one bound type to
another, there is no inheritance, even though the parameter type is
inherited.

You can do :

DerivedGenericType<AType> => BaseGenericType<AType>

....because the *generic* type is derived, but :

GenericType<DerivedType> => GenericType<BaseType>

....won't work because they are sibling classes rather than parent/child

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Apr 28 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.