How to create an object instance by its string type?

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?
 
S

Stoitcho Goutsev \(100\)

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.
 

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