About new object at run time using C#

Y

Yiu

String Information;
Information=touristSpotInformation;//touristSpotInformation is a class
name
Type myType=typeof(Information);
Object o=Activator.CreateInstance(myType);

But "Information" not a class;
How to change "Information" to class?

I have try to do this:
Type myType=Type.getType(Information);

But it have run time exception ArgumentNulException.

Have any good suggestion to create instance use class name which is
string in run time?
And after new instance, how to call the method of that class?

thx
 
N

Nicholas Paldino [.NET/C# MVP]

Yiu,

In order to do this, you will have to use the static CreateInstance
methods on the Activator class. This will allow you to create instances of
objects using the name of the type.

To call methods on the object, you will have to cast the object to a
type where you know the members (perhaps a base class, or an interface), or
use reflection.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top