473,803 Members | 3,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to instantiate an object using a string in C#

Hi ,

Need some help ! .
I am storing Classnames in an XML file and depending on
the user's choice of process want to :

1) Read the classname of that process from the XML file
2) Instantiate an object of that class from the string
received from the XML (which is the classname)

No problem reading the XML and getting the Classname as
string , but how to use that string and instantiate an
object of that class using C# .

Any help would be highly appreciated.

thanks
Prashant

Nov 13 '05 #1
2 22061
try this:

(cast to your favorite
object)System.R eflection.Activ ator.CreateInst ance("assembly name","type
name");

--
Jeff Langston
Director of Product Development
Cashless Systems, Inc.
MCP, MCAD

"Prashant" <pr************ @hotmail.com> wrote in message
news:09******** *************** *****@phx.gbl.. .
Hi ,

Need some help ! .
I am storing Classnames in an XML file and depending on
the user's choice of process want to :

1) Read the classname of that process from the XML file
2) Instantiate an object of that class from the string
received from the XML (which is the classname)

No problem reading the XML and getting the Classname as
string , but how to use that string and instantiate an
object of that class using C# .

Any help would be highly appreciated.

thanks
Prashant

Nov 13 '05 #2
Thanks everybody ,

It worked using Reflection . I did it by getting the
Executing Assembly and calling the CreateInstance method
for it . (passing the class name as string ).

Thanks
Prashant
-----Original Message-----
In Jeff's example that will be case sensitive on the class name, sowatch that. It is the easiest function to use and most of the timewill suit your problem perfectly if you maintain a controlled list ofavailable classes.

In the case of reading things programatically , etc., and where youmight not have control of case-senstitive input, you can also use anoverride with more parameters, one of which being a "IgnoreCase "boolean. That particular override happens to be a pain in the(*&$)*(@& to use, however.

"Jeff Langston" <je**********@c ashlesssystems. com> wrote

in message news:<eF******* *******@TK2MSFT NGP12.phx.gbl>. ..
try this:

(cast to your favorite
object)System.R eflection.Activ ator.CreateInst ance ("assembly name","type name");

--
Jeff Langston
Director of Product Development
Cashless Systems, Inc.
MCP, MCAD

"Prashant" <pr************ @hotmail.com> wrote in message
news:09******** *************** *****@phx.gbl.. .
> Hi ,
>
> Need some help ! .
> I am storing Classnames in an XML file and depending on > the user's choice of process want to :
>
> 1) Read the classname of that process from the XML file > 2) Instantiate an object of that class from the string
> received from the XML (which is the classname)
>
> No problem reading the XML and getting the Classname as > string , but how to use that string and instantiate an
> object of that class using C# .
>
> Any help would be highly appreciated.
>
> thanks
> Prashant
>

.

Nov 13 '05 #3

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

Similar topics

8
775
by: Carel Lotz | last post by:
H We have ported our VB 6 application into VB .NET but are still integrating with a few COM + applications written in VB6 running on our application server (Win 2000 Server). We have the proxies to link to the application server installed but we every now and then get a error when we try to make a call to a component running on the app server. The error message is something like Object reference not set to an instance of an objec at...
5
2378
by: Glenn Serpas | last post by:
I have Class A and Class B .. Class B has a private member that is a pointer to a Class A object. private: B *mypointer ; I instantiate the A object A* myobject new = A();
9
2635
by: the_grove_man | last post by:
I guess my question can go in two directions. I create applications that run multiple queries against a database. Generally speaking in the past I have used a Data Control (calling it dat1) making it invisible and have used this whenever i needed it. dat1.databasename = app.path & "mydatabase.mdb" dat1.recordsource = "my query" dat1.refresh
7
1997
by: J-T | last post by:
I can instantiate my object in my *ASP.NET* application in two ways: A) public sealed class RSSingleton { private static ReportingServiceProxy m_RsProxy=null; static RSSingleton() { m_RsProxy = new ReportingServiceProxy();
13
2320
by: Brian | last post by:
I have many similar classes in a project, one for each type of report my app can create. I want to instantiate them based on a value passed in by a scheduler module. Right now I have Sub RunReports(sReport) Select Case sReport Case "CRByDistrict" oReport = New CRByDistrict
2
1653
by: PIEBALD | last post by:
An XML Schema document is an XML document, and (in .net) an XML document can be loaded from a string. Is there a simple way to load an XML Schema document from a string? The closest I can see is to wrap the string in a stream and pass the stream to the constructor. Stepping back further, I might want to load the XML Schema into an XML document object and validate it to be sure it's a valid XML Schema before I try to instantiate the...
4
1686
by: GiBo | last post by:
Hi all, I have a class URI and a bunch of derived sub-classes for example HttpURI, FtpURI, HttpsURI, etc. (this is an example, I know there is module urllib & friends, however my actual problem however maps very well to this example). Now I want to pass a string to constructor of URI() and get an instance of one of the subclasses back. For example uri=URI('http://abcd/...') will make 'uri' an instance of HttpURI class, not instance of...
4
3667
by: Tomas | last post by:
A newbie question: How can I instantiate objects dynamically in VB.NET. E.g. I have the object 'Player' and I would like to instantiate it with the several instances (James, Gunner, etc.), without in advance knowing how many objects (employee1, employee2, etc) Dim player1 As New Persons.Players Dim player2 As New Persons.Players Dim player3 As New Persons.Players ....
1
2838
by: learning | last post by:
Hi how can I instaltiate a class and call its method. the class has non default constructor. all examples i see only with class of defatul constructor. I am trying to pull the unit test out from the product source code, but still want to execute them under nunit. I am trying this idea on nunit sample source code. Here is my class and the experiemental code: both money.cs and Imoney.cs is compiled to cs_money.dll. then I create another...
0
9699
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10542
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7600
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6840
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();...
0
5496
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...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
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
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2968
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.