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

Class Instantiation by name

How do i instantiate a class if I get the name of the class at runtime ?
I am looking for something like class.forname("classABC") in Java .

TIA
Mandar
Nov 22 '05 #1
2 1594
=?Utf-8?B?TWFuZGFy?= <an*******@discussions.microsoft.com> wrote:
How do i instantiate a class if I get the name of the class at runtime ?
I am looking for something like class.forname("classABC") in Java .


Well, Type.GetType is roughly the equivalent of Class.forName, but
Activator.CreateInstance is roughly the equivalent of
Class.newInstance.

Note that in order to get a Type object for a type in an assembly other
than either the calling one or mscorlb, you need to either load the
assembly yourself and use Assembly.GetType, or include the full name of
the type including assembly information in the call to Type.GetType.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
=?Utf-8?B?TWFuZGFy?= <an*******@discussions.microsoft.com> wrote:
How do i instantiate a class if I get the name of the class at runtime ?
I am looking for something like class.forname("classABC") in Java .


Well, Type.GetType is roughly the equivalent of Class.forName, but
Activator.CreateInstance is roughly the equivalent of
Class.newInstance.

Note that in order to get a Type object for a type in an assembly other
than either the calling one or mscorlb, you need to either load the
assembly yourself and use Assembly.GetType, or include the full name of
the type including assembly information in the call to Type.GetType.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #3

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

Similar topics

6
by: Ruud de Jong | last post by:
I have the situation where I need to construct the name of a static method, and then retrieve the corresponding function from a class object. I thought I could just use __getattribute__ for this...
25
by: John Harrison | last post by:
This code fails to compile on Comeau C++ and VC++ 7.1 (with language extensions disabled) template <class T> struct B { T b; }; template <class T>
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
2
by: Jonas Cord | last post by:
Hi, I'm trying to learn proxy classes in C++ so I've written the following code, trying to modify an example given in Deitel & Deitel's C++ book. I am trying to "hide" details of the original...
7
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename();...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
5
by: Slant | last post by:
Here's a question that most will have different answers to. I'm just dying to find a solution that seems halfway automated!! There really are two seperate issues which might be answered by the...
10
by: SpreadTooThin | last post by:
I am having trouble getting a piece of code to compile on VC++ 6.0... Compiles fine on MAC OS X under X-Code.... #include <list> class myClass { private: std::string name;...
4
by: ciccio | last post by:
Dear all, once again I stumbled upon the following puzzling problem. When having the following two files (see below), the gnu compiler compiles the file without a problem while the compiler...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.