Create Object from database stored class name

  • Thread starter Thread starter John Hughes
  • Start date Start date
J

John Hughes

Hi,

Is it possible to create an object from a stored (database,xml or variable)
name?

For instance I have 2 class names stored in my database. Depending on the
application settings I want to create an object based on the class name that
I store in the database.

Any ideas how to do this?
 
Hello

Here is some code:
string str = "System.String";

object[] parameters = new object[] { 'h', 5 };

object o = System.Activator.CreateInstance( Type.GetType( str ),
parameters );

System.Diagnostics.Debug.WriteLine( o.GetType().ToString() + " = " +
o.ToString() );



Activator.CreateInstance() can create an instance of an object of specified
type. If your object has parameterless constructor, then you can skip
parameters specification.

The above sample will create a String object, using the next constructor
CString(char c, int count).


--
With best regards,
Andrew


http://www.codeproject.com/script/profile/whos_who.asp?id=1181072
 
Back
Top