.NET API Reference

System.Type

Namespace: System.Reflection

Assembly: System.Runtime.dll

Summary

Represents type declarations of code objects that make up a program. This is the base class for all type declarations, and is used to represent classes, interfaces, arrays, value types, enumerated types, type parameters, generic type definitions, and open or closed constructed generic types.

Inheritance

System.ObjectSystem.Type

Remarks

The Type class provides information about the type, such as its members (methods, properties, fields, events), its attributes, and its base type. It is a fundamental part of reflection in .NET, allowing you to inspect and manipulate code at runtime.

You can obtain a Type object in several ways:

  • Using the typeof operator (e.g., typeof(string)).
  • Using the GetType() method on an object instance (e.g., myObject.GetType()).
  • Using the Type.GetType(string) method, which takes a fully qualified type name.

Members

The System.Type class has many members that provide access to type information and functionality. Below are some of the most commonly used ones.

Member Description
FullName
string
Gets the fully qualified name of the current type.
Name
string
Gets the name of the current type without the namespace.
Namespace
string
Gets the namespace of the current type.
IsPublic
bool
Gets a value indicating whether the type is public.
IsAbstract
bool
Gets a value indicating whether the type is abstract.
IsClass
bool
Gets a value indicating whether the type is a class.
IsEnum
bool
Gets a value indicating whether the type is an enumeration.
IsValueType
bool
Gets a value indicating whether the type is a value type.
BaseType
Type
Gets the direct base type of the current type.
GetMethods()
MethodInfo[]
Gets an array of the public methods defined on the current type.
GetProperties()
PropertyInfo[]
Gets an array of the public properties defined on the current type.
GetFields()
FieldInfo[]
Gets an array of the public fields defined on the current type.
MakeGenericType(Type[] typeArguments)
Type
Creates a new constructed generic type from the current generic type definition and the specified type arguments.

Example Usage


using System;
using System.Reflection;

public class Example
{
    public static void Main(string[] args)
    {
        // Get the Type of a string
        Type stringType = typeof(string);
        Console.WriteLine($"Type Name: {stringType.Name}");
        Console.WriteLine($"Full Name: {stringType.FullName}");
        Console.WriteLine($"Namespace: {stringType.Namespace}");
        Console.WriteLine($"Is Public: {stringType.IsPublic}");
        Console.WriteLine($"Is Class: {stringType.IsClass}");
        Console.WriteLine($"Base Type: {stringType.BaseType?.Name}"); // Nullable for Object

        Console.WriteLine("\n--- Public Methods ---");
        MethodInfo[] methods = stringType.GetMethods();
        foreach (var method in methods.Take(5)) // Show first 5 for brevity
        {
            Console.WriteLine($"- {method.Name}");
        }

        // Get the Type of an object instance
        int number = 123;
        Type intType = number.GetType();
        Console.WriteLine($"\nType of integer instance: {intType.FullName}");

        // Get a Type by name (requires fully qualified name including assembly)
        // Type listType = Type.GetType("System.Collections.Generic.List`1, System.Private.CoreLib");
        // if (listType != null && listType.IsGenericTypeDefinition)
        // {
        //     Console.WriteLine("\nSuccessfully found List generic definition.");
        // }
    }
}