Q: Extracting System.Type from non-instantiated class defs?

  • Thread starter Thread starter Sky
  • Start date Start date
S

Sky

Hello:

for the last month, I've been blindly using syntax like:
System.Type tType = System.Type.GetType("MyNS.MyClass");

To get the type from a class def, when I don't have an instantiated object
to work with:

tType = o.GetType();

But this going through a string to get to the class def is really verbose...
And, I've noticed that you can get into a lot of trouble when working with
several assemblies (the IDE can always find the class def, but at run-time,
but at runtime if the classdef is in another assembly it will return
NULL...you have to itterate through all the assemblies to find the def...).

Therefore, my question: Is there any another syntax, that looks closer to
the following, so that I know that the compiler will find it for sure
(rather than having to go through the String method?)

MyNameSpace.MyClass.GetType();

Thanks in advance!
 
Sky said:
System.Type tType = System.Type.GetType("MyNS.MyClass"); : :
Therefore, my question: Is there any another syntax, that looks closer to
the following, so that I know that the compiler will find it for sure

System.Type tType = typeof( MyNS.MyClass);

Or if you're within scope of a "using MyNS;" then simply typeof( MyClass) will give
you the Type.


Derek Harmon
 
Thanks Derek!

Whew! Exactly what I needed!


Derek Harmon said:
System.Type tType = typeof( MyNS.MyClass);

Or if you're within scope of a "using MyNS;" then simply typeof( MyClass) will give
you the Type.


Derek Harmon
 
Back
Top