TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Alex Paterson
NA
7
3.6k
Creating an object from type / casting into correct type from list of baseclasses
Dec 12 2010 6:07 PM
Hi all,
Firstly, I apologise from resorting to creating a thread for this topic. I have been trying to tackle this for a while and have searched/experimented with a number of techniques but am unable to get it to work. I appreciate that I have probably been given the answer in something I have read, but I haven't been sharp enough to see it. In addition, there is a reason why I think this may not be possible, but my knowledge of c# is not sufficient for me to construct a concrete argument
Anyway TL:DR...
In a program I am creating I have a list which contains a variety of classes which all derive from the same baseclass. (This list is of type baseclass).
I wish to run though this list and either cast these baseclass reference objects into their derived classes OR create a new object and cast/copy the derived objects held in the list of baseclasses.
I think it may not be possible as C# will not know which class I need to use until runtime (edit: anything to do with c# being strongly typed?)..however, given that I can read the derived type in the list of baseclasses (using List[i].GetType), I am unsure why I cannot go ahead and make a new object.
I will try not bore you with all the incorrect experiments I have tried, but some of the first were:
foreach(baseclass bc in list)
{
Type derivedtype = bc.GetType();
//Here I can writline derivedtype .ToString() and get the name of the derived class, so seems to be holding it in there
/
/Then something like..
derivedtype derivedclassobject = new derivedtype () //Doesnt work
//or
bc = bc as derivedtype
//or
object o = (derivedtype )bc
//as well as things such using
Activator.CreateInstance(...);
}
Suffice to say, I'm not having any luck. I'm also sure I am showing a fundamental misunderstanding of a core concept in c# programming. I just dabble as a hobbyist unfortunately.
If anyone can tell me straight up that im trying to do some impossible, then please do. If anyone knows how to get this done, a clear explanation would be worth its weight in gold (edit: sorry, that phrase doesnt work in this case, id be very appreciative!).
Ive worked around it for now, its part of the loading/saving mechanisms for my objects in a stack used in a windows 7 phone game engine. Im just giving each object a string holding its type, and then switching this string and writing out methods explicitly for creating all the derived object types, as I rebuild the game stack from serialised saved data. I'd like to not have the user of the game engine (ie me lol) to have to remember to explicitly modify this part of the engine to handle any objects they decide to make :)
I'm wondering if a solution exists using generics, perhaps it can make better use of the type data I can pull from the objects in the list. Im going to read up on them now as they are new to me (ive come back to c# programming recently from pre c# 2.0 :) )
anyway enough excuses, thanks in advance for any help
EDIT:: Looks like it might require reflection? the activator.creatinstance() just gets me a type object, which i still need to cast! we end up further away. More and more i dont think it will work, i just dont see c# letting me do it without knowing what it will be first. again with strongly typed stuff..Is there another way to approach this sort of stuff?
Reply
Answers (
5
)
How to update listbox after inserting new row
REGARDING COMBOBOX