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.LoadFr om(derivedFileN ame);
Type derivedType = asm.GetType(der ivedTypeName);
bool argument = false;
object args = { argument };
object derivedObject = Activator.Creat eInstance(deriv edType, args);
BaseType base = (BaseType)deriv edObject;
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.Creat eInstance(strin g 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
ActivationAttri bute 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. 3 5463
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 incomprehensibl e 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.
Rob Richardson <no*******@n2ne t.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 incomprehensibl e to me.
See http://www.pobox.com/~skeet/csharp/plugin.html for a couple of
ways.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
Here's what I'm generally trying to achieve:
I'm building (trying to anyway) a serialization library. Here's my design:
|
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; /* the same as Object */
|
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 obj=Activator.CreateInstance(strAssemblyName, "MyClass");
//Now, I want to do something like ((MyClass)obj).Method
//Can I do this?
|
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 flags = (BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
//Iterate through the Assembly to find Class with a Public Interface.
|
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 5), but fails on IIS 6 running on a Win2003 server.
The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide
some common functionality. The Page_Load handler the failing webpage starts out like this:
...
| |
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 variable (targetId) in to the
usercontrol (IntergySite.aspx) by calling its setter method.
Currently, I am using if-then-else and hardcoded the User Control Object to
do casting and call the setter method.
Question: Is there any way I could load,...
|
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 shows a scenario where it was determined that cDerivedA
is needed based on a configuration file.
Dim lInstance As New cBase()
|
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 that several classes inherit from. From
time to time I need to add new inherited types so I want something that
is as versatile as possible, preferably not requiring naming all
subtypes in a switch statement.
|
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 objCatalog = null;
COMAdminCatalogCollection objApplicationsColl = null;
COMAdminCatalogObject objApp = null;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |