Using GetType to retrieve Type objects

K

KK

Consider the following code block:
Rectangle r = new Rectangle();
Type t0 = r.GetType()
Type t1 = typeof(System.Drawing.Rectangle);
Type t2 = Type.GetType("System.Drawing.Rectangle");
Type t3 = Type.GetType(r.GetType().ToString());
Console.WriteLine("Results:\nt0: {0}\nt1: {1}\nt2: {2}\nt3: {3}", t0, t1,
t2, t3);
<<<<<

While t0 and t1 print the results one might expect, t2 and t3 return null.
I'm really perplexed by this. If I supply the fully qualified type name,
isn't that enough to uniquely determine the type? Apparently not! My
question is this: given only a string containing fully qualified type name
(e.g. "System.Drawing.Rectangle"), is there a consistent way to get the Type
object that the string corresponds to that will work with most types?

Thanks for any help you can give!
 
E

Eric

Jeff,

I love your OOP tutorial for C#. I've recommended it many times.

I hope you'll add some new sections on v2.0 features someday!

Have you considered writing a book?

Eric
 

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