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

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 22046
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
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...
5
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
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)...
7
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() {...
13
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...
2
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...
4
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...
4
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...
1
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
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:
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.