About new object at run time using C#

  • Thread starter Thread starter Yiu
  • Start date Start date
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
 
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.
 
Back
Top