473,408 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

generic type and cast

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
1 1747
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: 3nc0d3d | last post by:
I have needed to cast a pointer with different types for get a value. I have try to use generic, but there are cast exception. For example the following method: generic<typename T> void...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
3
by: =?Utf-8?B?TWlydHVs?= | last post by:
Hi I'm currently working with vbscripting through MSScriptControl. We have shared some of our objects that should be available for scripting. Some of the functions of these objects will return...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
15
by: Anthony Paul | last post by:
Let's say that I would like a generic type that supports Min/Max properties and can be double or integer or even datetime if need be, something flexible. So I go about creating the following...
3
by: Dave | last post by:
I'm calling string.Split() producing output string. I need direct access to its enumerator, but would greatly prefer an enumerator strings and not object types (as my parsing is unsafe casting...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
15
by: Lloyd Dupont | last post by:
Don't mistake generic type for what you would like them to be!! IFoo<Ahas nothing in common with IFoo<B>! They are completely different type create dynamically at runtime. What you ask is a...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.