Activating an instance of a class in a referenced assembly

  • Thread starter Thread starter Scott Hodson
  • Start date Start date
S

Scott Hodson

I'm trying to use Type.GetType() so I can create an instance of a class I
have defined in a different, yet referenced, assembly but it doesn't seem to
be able to find the type. How can I get a type object of a class in
another assembly?

For example, if you reference the System.Xml namespace, try this
Type importType = Type.GetType("System.Xml.XmlException", true);

It will throw an exception. I see that there is also the
GetTypeFromProgID() and GetTypeFromCLSID() methods in the Type class but
these seem to only apply to registered COM objects.
 
For example, if you reference the System.Xml namespace, try this
Type importType = Type.GetType("System.Xml.XmlException", true);

this is not the full type name. try:

Type t = typeof( XmlException );
MessageBox.Show( t.AssemblyQualifiedName );

to see the full type name. you can use the full type name to get the type by
Type.GetType( "......." );


remember that whenever the type is statically known, you can use

Type t = typeof( _type_name_ );
instead of
Type t = Type.GetType( _fully_qualified_type_name );

regards,
Wiktor
 
No, you are incorrect. The sample code I gave you, if run, will throw an
exception. If I take out the true parameter on the call to GetType I won't
get an exception, but the reference "importType" will be null.

And I can't do this statically, I need do do this dynamically with a string.
I need a way to dynamically instantiate an instance of a class that resides
in another assembly, and GetType() is not able to find it, it seems to only
work for classes in the same assemble as the caller.
 
Back
Top