473,569 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)S ystem.createObj ect("EnteredCla ssName");

of course i assume that EnteredClassNam e implements IMyInterface.

Nov 15 '05 #1
2 5919
Lukasz Lacki <la***@hot.pl.n o-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)S ystem.createObj ect("EnteredCla ssName");

of course i assume that EnteredClassNam e implements IMyInterface.


I think you're looking for Activator.Creat eInstance.

--
Jon Skeet - <sk***@pobox.co m>
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.n o-spam> wrote in message
news:3f******@n ews.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)S ystem.createObj ect("EnteredCla ssName");

of course i assume that EnteredClassNam e implements IMyInterface.


Use:

IMyInterface obj = (IMyInterface)A ctivator.Create Instance("Assem blyname",
"ClassName").Un wrap();
Arild
Nov 15 '05 #3

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

Similar topics

3
3623
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 constructor of a type object (Button(), in this case) ??? Thank you and God bless.
1
3687
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 using reflection since they are basically numeric data. But for structures (or for that matter classes), I am not sure how to create a new structure...
2
1319
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 regular dll with additional remotable feature. b) Host Application, which is a listener listens for remote clients of a remotable type
7
2206
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 based on the format specifier in the Format field. The problem is, since the Value field is a string, it doesnt properly "format". So I need to...
16
4743
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? Thanks very much in advance, Daniel :)
9
6381
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 = CType(e.Item.FindControl("edit_name"), TextBox)
13
2414
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 single Object by itself, I'd like to assign a reference to the class type and call it later without knowing the derived type. Example:
15
7078
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 generic interface : *note : in reality I implement the IComparable and IEquatable generic interfaces and associated overriden methods, but I've cut...
9
3444
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
4919
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
7697
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, 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...
0
7612
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...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7968
tracyyun
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...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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...
0
3653
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...
1
2113
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
0
937
bsmnconsultancy
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...

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.