Discovering Types

Now that you know we can query lots of properties of types, we should briefly revisit how we can obtain the Type objects themselves. There are three main ways to do so, the first of which we’ve already covered: use of the typeof operator in C#. Although this way is the most direct, it has little value in scenarios where you’re really about to find out about type information you didn’t know about at compile time:

var stringType = typeof(string /* you did know the type name, didn't you? */);var intType = typeof(int /* same story here */);

Because every object floating around in the CLR’s managed memory space has a type, it’s obvious one should be able to obtain a System.Type instance from any object. ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.