473,386 Members | 1,842 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.

create object, knowing it's type at run-time?

how can i create object declaring it's class during run-time?

fe. i ask user for classname.
after he enters, i'll need to do something like this:

IMyInterface obj = (IMyInterface)System.createObject("EnteredClassNam e");

of course i assume that EnteredClassName implements IMyInterface.

Nov 15 '05 #1
2 5911
Lukasz Lacki <la***@hot.pl.no-spam> wrote:
how can i create object declaring it's class during run-time?

fe. i ask user for classname.
after he enters, i'll need to do something like this:

IMyInterface obj = (IMyInterface)System.createObject("EnteredClassNam e");

of course i assume that EnteredClassName implements IMyInterface.


I think you're looking for Activator.CreateInstance.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
"Lukasz Lacki" <la***@hot.pl.no-spam> wrote in message
news:3f******@news.home.net.pl...
how can i create object declaring it's class during run-time?

fe. i ask user for classname.
after he enters, i'll need to do something like this:

IMyInterface obj = (IMyInterface)System.createObject("EnteredClassNam e");

of course i assume that EnteredClassName implements IMyInterface.


Use:

IMyInterface obj = (IMyInterface)Activator.CreateInstance("Assemblyna me",
"ClassName").Unwrap();
Arild
Nov 15 '05 #3

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

Similar topics

3
by: Marco | last post by:
Howdy! Given that: Type type = Type.GetType("System.Windows.Forms.Button"); Question: How do I obtain a Button object by only knowing the type object. In other words, how do I fire the...
1
by: Saranjit Singh | last post by:
Is there any way I can enumerate the properties of an object at runtime and assign values to each property of the object depending on its data type? For primitive data types, this is easily done...
2
by: Anson | last post by:
Hello, I would like to clarify something about Remoting Object in .NET. Knowing from MSDN that in order for Remote Object to work, I need 3 components. a) RemotableType object, just like a...
7
by: Dave Taylor | last post by:
I have a DataTable with three string columns, "Value", "Format" and "Type" However, some of the rows contain numbers and dates in the Value field. So I would like to be able to format the output...
16
by: danielbuus | last post by:
....or, to put it perhaps more communicative, something like this: Type someObjectsType = someObject.GetType(); someObjectsType newObject = new someObjectsType(); Is this possible? If so, how?...
9
by: Jim in Arizona | last post by:
I get this error: Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText' to type 'System.Web.UI.WebControls.TextBox'. Using this code: Dim test3 As TextBox test3 =...
13
by: docschnipp | last post by:
Hi, I have a bunch of object derived from the same base class. They all share the same constructor with some parameters. Now, instead of using a large switch() statement where I call every...
15
by: Anthony Paul | last post by:
Let's say that I would like a generic type that supports Min/Max properties and can be double or integer or even datetime if need be, something flexible. So I go about creating the following...
9
by: Grant Schenck | last post by:
Hello, I have a base class with several derived classes: ScriptBase + ScriptCallInbound + ScriptCallOutbound I then have another class: Line
53
by: Aaron Gray | last post by:
Due to M$'s stupidity in not making DOMElements first class citizens the following will not work :- function isElement( o) { return o instanceof Element } It works for FF, Opera and Safari.
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.