BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Reflection
.NET 1.1+

The System.Type Class

The second part of the Reflection tutorial looks at the System.Type class. This class can be used to represent a type declaration, such as that of a class or structure, and obtain information about that type and its members.

Common Type Properties

In the remainder of this article we'll look at some of the more commonly used Type properties. We'll expand upon this later in the tutorial. All of the examples will obtain a small piece of information about one of our custom types or about a .NET framework type. To obtain the Type instances we need, replace the body of the Main method with the following:

Type publicClass = typeof(PublicClass);
Type privateNestedStruct = Type.GetType("ReflectionTest.PublicClass+PrivateNestedStruct");
Type publicNestedClass = typeof(PublicClass.PublicNestedClass);
Type abstractClass = typeof(AbstractClass);
Type internalInterface = typeof(InternalInterface);
Type enumeration = typeof(Enumeration);
Type primitive = typeof(int);
Type array = typeof(int[]);

Identification Properties

There are a number of properties that identify a type using a globally unique identifier (GUID), the fully qualified name of a type or a part thereof. The AssemblyQualifiedName property returns the fully qualified name of the type, including additional information about the assembly it is defined within. The FullName property gives the fully qualified name without the assembly information. Name returns the name of the type only and Namespace gives a string containing the namespace that the class resides within. Finally, the GUID property returns a unique identifier for the type. Usually this GUID is generated automatically. However, it can be specified by adding a Guid attribute to the type.

The following shows the identification properties for the PublicClass type. The GUID value will be different if you run the code on your own computer.

Console.WriteLine(publicClass.AssemblyQualifiedName);
Console.WriteLine(publicClass.FullName);
Console.WriteLine(publicClass.Name);
Console.WriteLine(publicClass.Namespace);
Console.WriteLine(publicClass.GUID);

/* OUTPUT

ReflectionTest.PublicClass, ReflectionTest, Version=1.0.0.0, Culture=neutral,
 PublicKeyToken=null
ReflectionTest.PublicClass
PublicClass
ReflectionTest
10f82946-bd28-30e9-b4a5-c23b15a4e2b8

*/

Scope Properties

Four handy properties allow you to determine the scope of a type. When using reflection you can obtain information about types of any scope, including public, internal and private elements. To determine if a type if public or not, you can check one of the two Boolean properties, IsPublic and IsNotPublic. For nested types you can check if an item is public or private with IsNestedPrivate band IsNestedPublic.

Console.WriteLine(publicClass.IsPublic);                // true
Console.WriteLine(publicClass.IsNotPublic);             // false
Console.WriteLine(publicClass.IsNestedPublic);          // false
Console.WriteLine(publicClass.IsNestedPrivate);         // false

Console.WriteLine(internalInterface.IsPublic);          // false
Console.WriteLine(internalInterface.IsNotPublic);       // true

Console.WriteLine(publicNestedClass.IsNestedPublic);    // true
Console.WriteLine(publicNestedClass.IsNestedPrivate);   // false

Console.WriteLine(privateNestedStruct.IsNestedPublic);  // false
Console.WriteLine(privateNestedStruct.IsNestedPrivate); // true

Declaration Type Properties

Several properties can be used to determine the kind of type in a Type instance. IsClass returns true if the type is a class. IsInterface returns true for interfaces. IsValueType returns true for structures, enumerations and native types that are value types but false for reference types. IsPrimitive returns true if the type being reflected is a Boolean, byte, sbyte, short, ushort, int, uint, long, ulong, char, single, double, IntPtr or UIntPtr, which are the primitive types. If you are examining an enumeration, IsEnum is true and for arrays, IsArray returns true.

Console.WriteLine(publicClass.IsClass);                 // true
Console.WriteLine(internalInterface.IsClass);           // false

Console.WriteLine(publicNestedClass.IsInterface);       // false
Console.WriteLine(internalInterface.IsInterface);       // true

Console.WriteLine(publicClass.IsValueType);             // false
Console.WriteLine(privateNestedStruct.IsValueType);     // true

Console.WriteLine(privateNestedStruct.IsPrimitive);     // false
Console.WriteLine(primitive.IsPrimitive);               // true

Console.WriteLine(abstractClass.IsEnum);                // false
Console.WriteLine(enumeration.IsEnum);                  // true

Console.WriteLine(abstractClass.IsArray);               // false
Console.WriteLine(array.IsArray);                       // true

Miscellaneous Properties

Three further commonly used properties are available. The first two describe how the class can be inherited. Abstract classes cause the IsAbstract property to return true. Classes that are declared as sealed have a true IsSealed result. The final property is IsSerializable. As the name suggests, this returns true for types that may be serialized and false otherwise.

Console.WriteLine(abstractClass.IsAbstract);            // true
Console.WriteLine(publicClass.IsAbstract);              // false

Console.WriteLine(publicNestedClass.IsSealed);          // true
Console.WriteLine(abstractClass.IsSealed);              // false

Console.WriteLine(primitive.IsSerializable);            // true
Console.WriteLine(abstractClass.IsSerializable);        // false
5 February 2012