Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 3rd, 2009, 08:20 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Question Why can't do this in c#????

Hi people..

Unless I lost some brain somewhere, I don't understand why I can't do this..

I have an abstract class (say class1) and a child class (childclass).

I'm loading the dll with the childclass by reflection, calling up the correct class, and trying to instanciate it using a cast to class1.

Code:
Class1 class1 = (Class1) Activator.CreateInstance(childclass)
childclass has the correct type that I need to activate.

This code, throw an error that can't convert childclass to class1. Isn't this possible??? Childclass can't be converted to his base class?? or it's because it is an abstract class??? (the one I'm trying to convert it to??)
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old May 3rd, 2009, 11:29 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I'm going to play with this, but in the meantime...

Why not write a little debugging code?

Code:
Object o = Activator.CreateInstance(childclass);
Reponse.Write( o.ToString() + "<hr>" + o.GetType() ); // or use console output
 
Old May 3rd, 2009, 11:31 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Oh, never mind...you said the error is "can't convert childclass to class1", so that means that the CreateInstance *did* properly return a childclass instance, as you would expect.

So if that code fails, then why wouldn't
Code:
childclass c = new childclass( );
Class1 c1 = (Class1) c;
*also* fail??
The Following User Says Thank You to Old Pedant For This Useful Post:
gbianchi (May 4th, 2009)
 
Old May 4th, 2009, 08:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Lol... There is an underlying problem with childclass. The contructor is throwing an exception.. I don't see any reference to it in my side, and c# still build the class (?), but the problem is there. I will try to solve it an come back. Your second idea worked for me to see the error. :)
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old May 4th, 2009, 10:06 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Ok, I think I found out the problem.

The cast is valid if I have the two classes referenced on my solution.

since I'm loading one of them using reflection, the create instance returns a class of type object, that doesn't have inherits info on it. So I believe it can't do the cast because it does not have info on how to cast it.

So I have a new question. Any one know if it is posible to do the cast in any way??? I want to avoid using invoke (because I know what I need to invoke).
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.