How Can I create an object instance from a string

G

Guest

I would like to create an instance of System.Windows.Form.TextBox
but I just have the name of the objec in a string like that:

string strType = "System.Windows.Form.TextBox";

I tryed myOgject = CreateInstance(Type.GetType(strType))
but it does not work because Type.GetType(strType) rturn null.

Does anybody can help me???
 
T

TheSteph

Try this :
using System.Reflection;
//...
//...
string strTypeName = "System.Windows.Form.TextBox";
Object Obj = Activator.CreateInstance(strTypeName)


Steph
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

IIRC the string being passed need to have more details about the type being
instantiated, this was the reason I never used it. I have used
AppDomain.CreateInstance and it does work great
 
G

Guest

The problem could be in that yoy have no assembly with TextBox loaded

use smth like this

string strType = "System.Windows.Forms.TextBox";
Assembly asm = Assembly.LoadWithPartialName("System.Windows.Forms");
Type tp = asm.GetType(strType);
object obj = Activator.CreateInstance(tp);

PS: LoadWithPartialName is depricated in .net 2.0

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche




TheSteph said:
Try this :
using System.Reflection;
//...
//...
string strTypeName = "System.Windows.Form.TextBox";
Object Obj = Activator.CreateInstance(strTypeName)


Steph
 

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