By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,673 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,673 IT Pros & Developers. It's quick & easy.

How to instantiate an object using a string in C#

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
try this:

(cast to your favorite
object)System.Reflection.Activator.CreateInstance( "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

P: n/a
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**********@cashlesssystems.com> wrote

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

(cast to your favorite
object)System.Reflection.Activator.CreateInstance ("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 discussion thread is closed

Replies have been disabled for this discussion.