Typecast to a type from just the string representation of the type name

Posted by Water Cooler v2 on Stack Overflow See other posts from Stack Overflow or by Water Cooler v2
Published on 2010-04-15T09:01:11Z Indexed on 2010/04/15 9:03 UTC
Read the original article Hit count: 280

sTypeName = ... //do some string stuff here to get the name of the type

/*
The Assembly.CreateInstance function returns a type
of System.object. I want to type cast it to 
the type whose name is sTypeName.

assembly.CreateInstance(sTypeName)

So, in effect I want to do something like:

*/

assembly.CreateInstance(sTypeName) as Type.GetType(sTypeName);

How do I do that? And, what do I take on the left side of the assignment expression, assuming this is C# 2.0. I don't have the var keyword.

© Stack Overflow or respective owner

Related posts about reflection

Related posts about system.reflection