Creating unknown objects at runtime

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Howdy

I need to create an instance of a subclass of one of my classes, but I won't know the name of the subclass until runtime. How can I create an instance of a class when all I have is the class' name in a string

Also, these subclasses will be uploaded to a website (by clients) and used as part of an ASP.NET page, so I need to make sure they don't contain any malicious code. If I was to scan the .cs files for the word System and disallow any files that contain it, would this be enough to stop any harmful code being uploaded and run on the webserver

Cheers
LittleC
 
Hi,

On your first issue user Reflection. Chekc the code below:

Type t = Type.GetType("MyClass");
MyClass obj = (MyClass) Activator.CreateInstance(t);


Second issue, you need to use Code Access Security (search the MSDN - you
will find a lot of material).

hth

Fitim Skenderi



littlecharva said:
Howdy,

I need to create an instance of a subclass of one of my classes, but I
won't know the name of the subclass until runtime. How can I create an
instance of a class when all I have is the class' name in a string?
Also, these subclasses will be uploaded to a website (by clients) and used
as part of an ASP.NET page, so I need to make sure they don't contain any
malicious code. If I was to scan the .cs files for the word System and
disallow any files that contain it, would this be enough to stop any harmful
code being uploaded and run on the webserver?
 
Back
Top