How to create an object instance by its string type?

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

Guest

I have a list of System.Type and I wish to create an instance for each of
this types.

string[] types = "System.Boolean", "System.Int32", "System.Single",
"System.Double", "System.String", "System.DateTime"};

System.Type type;

for( int i=0; i<types .Length; ++i )
{
type = System.Type.GetType(rowTypes);

// Missing code in here...
}


Can anybody now how can I do that?
 
Sharon,

You either use reflection or you can use Activator.CreateInstance method
providing the Type object or strings for the type name and declaring
assembly.

With reflection you need to call Type.GetContructor and then call Invoke
method on the returned ConstructorInfo object.
 
Back
Top