PC Review


Reply
Thread Tools Rate Thread

Cast from derived to base fails after Activator.CreateInstance() call

 
 
Rob Richardson
Guest
Posts: n/a
 
      29th Dec 2004
Greetings!

I am trying to make it possible for new derived classes of an object to be
used by my application without rebuilding the application. The new classes
will be made known to my application by storing the file name, assembly name
and class name for them in a database. The code to create an instance of
the object will look something like the following:

Assembly asm = Assembly.LoadFrom(derivedFileName);
Type derivedType = asm.GetType(derivedTypeName);
bool argument = false;
object args = { argument };
object derivedObject = Activator.CreateInstance(derivedType, args);
BaseType base = (BaseType)derivedObject;

This code succeeds through the second-last line. derivedObject is created,
and the debug watch window shows it to be an object of type derivedType.
However, the last line fails with an invalid cast exception. If I create
derivedObject merely by:

object derivedObject = new derivedType();

then the cast to BaseType succeeds.

Why does the cast fail, and what can I do to use my derived object as though
it were a base object?

Thanks very much!

Rob

P.S. I can use use Activator.CreateInstance(string assemblyName, string
typeName), but that limits me to the default constructor. The only way I
found to use names instead of types was to use the fullest version of
CreateInstance(). But that version requires an array of at least one
ActivationAttribute object, and I could find no information at all on what
that is. The only examples I could find seemed to indicate that it was an
indication of the location of a remote object, and I don't want to activate
my object remotely.


 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      29th Dec 2004
Rob,

>Assembly asm = Assembly.LoadFrom(derivedFileName);


See

http://gotdotnet.com/team/clr/LoadFromIsolation.aspx
http://blogs.msdn.com/suzcook/



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Rob Richardson
Guest
Posts: n/a
 
      29th Dec 2004
Mattias,

Thanks very much for your reply. Now that I've read those, the difference
makes sense.

But what do I do about it? I want to create an instance of a derived class
at run time, when all I know about it is the assembly name and class name,
and then I want to use that instance through a variable of a base class type
that is known at compile time. There is probably a way to do it using
binding contexts or some such thing, but this is one area (of many) in which
..Net is completely incomprehensible to me.

Rob

P.S. I have just amended an old saying of mine:

C++ lets you control how you are going to shoot yourself in the foot.
Visual Basic hides all the ways it shoots you in the foot.
..Net makes you jump through 13,000 hoops before you shoot yourself in the
foot.


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      29th Dec 2004
Rob Richardson <(E-Mail Removed)> wrote:
> Thanks very much for your reply. Now that I've read those, the difference
> makes sense.
>
> But what do I do about it? I want to create an instance of a derived class
> at run time, when all I know about it is the assembly name and class name,
> and then I want to use that instance through a variable of a base class type
> that is known at compile time. There is probably a way to do it using
> binding contexts or some such thing, but this is one area (of many) in which
> .Net is completely incomprehensible to me.


See http://www.pobox.com/~skeet/csharp/plugin.html for a couple of
ways.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid cast from base to derived INeedADip Microsoft C# .NET 10 18th Sep 2007 03:03 PM
Cast from parent(base) to son(derived) class? =?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno Microsoft C# .NET 11 19th Dec 2006 09:13 PM
Difference Between assembly.CreateInstance and System.Activator.CreateInstance John Jenkins Microsoft C# .NET 1 14th Oct 2004 10:00 PM
Difference Between CurrentDomain.CreateInstance and Activator.CreateInstance b0yce Microsoft Dot NET 0 23rd Mar 2004 03:56 PM
Cast and Activator.CreateInstance Vincent Lascaux Microsoft C# .NET 1 29th Jul 2003 03:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:34 AM.