// Obtain type information for a type within an external assembly.
Type t = Type.GetType("CarLibrary.SportsCar, CarLibrary");
As well, do know that the string passed into Type.GetType() may specify a plus token (+) to
denote a
nested type. Assume you wish to obtain type information for an enumeration (SpyOptions)
nested within a class named JamesBondCar. To do so, you would write the following:
// Obtain type information for a nested enumeration
// within the current assembly.
Type t = Type.GetType("CarLibrary.JamesBondCar+SpyOptions");
Obtaining a Type Reference Using typeof()
The final way to obtain type information is using the C# typeof operator:
// Get the Type using typeof.
Type t = typeof(SportsCar);
Like Type.GetType(), the typeof operator is helpful in that you do not need to first create an
object instance to extract type information. However, your code base must still have compile-time
knowledge of the type you are interested in examining, as typeof expects the strongly typed name of
the type, rather than a textual representation of the type.
Do'stlaringiz bilan baham: |