473,322 Members | 1,352 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,322 software developers and data experts.

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

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.
Nov 16 '05 #1
3 5395
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.
Nov 16 '05 #2
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.
Nov 16 '05 #3
Rob Richardson <no*******@n2net.net> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
11
by: Alberto Giménez | last post by:
Hi, I've seen some object oriented programming bits out there and i'm not sure if they're legal. For example: struct Object { int field1; int field2; }; struct SubObject { int field1; /*...
3
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
3
by: System.Reflection Activator | last post by:
************************************** //Load the Assembly Assembly a = Assembly.LoadFrom(sAssembly); //Get Types so we can Identify the Interface. Type mytypes = a.GetTypes(); BindingFlags...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
4
by: Joe HM | last post by:
Hello - I have a Base Class where I want a New() implemented that can be called from the outside. This New() should create an instance of the appropriate cDerivedX Class ... The following...
4
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class...
3
by: aljamala | last post by:
Hello, I have the following method to help me in installing some COM components onto the machine...below is a snippet that is causing the problem... Type objType = null; COMAdminCatalog...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.