generic type and cast

T

Tommaso Caldarola

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
 
J

Joanna Carter [TeamB]

"Tommaso Caldarola" <[email protected]> a écrit dans le message de
[email protected]...

| 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top