Dynamic cretation of object

  • Thread starter Thread starter Andy
  • Start date Start date
A

Andy

I'm trying to dynamically create object in runtime, here is code example:

string name = "System.Xml.XmlTextWriter"; // this is in fact read from
Database
System.Type t = System.Type.GetType(name);
System.Object ob = System.Activator.CreateInstance(t);

When type name is from another namespace then System, GetType method return
null. What is wrong?
TIA
 
Andy said:
I'm trying to dynamically create object in runtime, here is code example:

string name = "System.Xml.XmlTextWriter"; // this is in fact read from
Database
System.Type t = System.Type.GetType(name);
System.Object ob = System.Activator.CreateInstance(t);

When type name is from another namespace then System, GetType method return
null. What is wrong?

Your use of Type.GetType is wrong. As the docs say, only the current
assembly and mscorlib are searched unless you give a *full* type name
which includes the assembly name.

You either need to specify the full type name, or load/find the
appropriate assembly and call Assembly.GetType.
 

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

Back
Top