Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Reflection, Attributes and Metadata

The Type class

Reference for System.Type in C#: typeof and GetType, names, base types, interfaces, generics, and listing properties, fields and methods with BindingFlags.

System.Type is the object that describes a type at run time: its name, namespace, base type, interfaces, members and generic arguments. You get one with the typeof(T) operator when you know the type at compile time, or with obj.GetType() when you only have an object. Type is the entry point of reflection, and from it you reach PropertyInfo, MethodInfo, FieldInfo, ConstructorInfo and the attributes applied to the type.

A Type object is unique per type within a loaded context, so == and Equals work for comparing types. GetType() returns the actual run-time type, while typeof names a type you wrote in the source.

C#
Type t = typeof(List<int>);
Type u = "hello".GetType();   // System.String

Try it

C# Names, base types and interfaces

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Listing members with BindingFlags

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public abstract class Type : MemberInfo, IReflect
{
    public static Type? GetType(string typeName);                // by (assembly-qualified) name
    public Type? BaseType { get; }
    public PropertyInfo[] GetProperties(BindingFlags flags = Public | Instance | Static);
    public MethodInfo? GetMethod(string name, BindingFlags flags = Public | Instance | Static);
    public bool IsAssignableFrom(Type? c);
    public Type MakeGenericType(params Type[] typeArguments);
}
Member Description
Name, FullName, Namespace Simple name, namespace-qualified name and namespace. FullName of a constructed generic type also lists the assembly of every type argument.
BaseType, GetInterfaces() Direct base class (null for object and interfaces) and all implemented interfaces.
IsClass, IsValueType, IsInterface, IsEnum, IsAbstract, IsSealed, IsGenericType Classification flags.
GetProperties, GetFields, GetMethods, GetConstructors, GetEvents, GetMembers Return arrays of member descriptions, filtered by BindingFlags.
GetProperty(name), GetMethod(name), GetField(name) Return a single member, or null if there is none.
GetGenericArguments(), GetGenericTypeDefinition(), MakeGenericType(...) Inspect or build generic types.
IsAssignableFrom(t), IsSubclassOf(t) Test inheritance relationships.
GetElementType(), GetArrayRank() Describe arrays, pointers and by-ref types.

Returns: a Type, or a member description. Single-member lookups return null when nothing matches.

Errors: GetMethod(name) throws AmbiguousMatchException when several overloads share the name and no parameter types are given. Type.GetType("Name") returns null for unknown names unless throwOnError: true. MakeGenericType throws ArgumentException if the arguments violate constraints.

Usage notes

  • Default flags are public only. GetMethods() without arguments returns public instance and static members, including inherited ones. Add NonPublic for private members and DeclaredOnly to skip inherited ones.
  • Order is not guaranteed. Member arrays come back in no documented order; sort them (as in the example) before showing or comparing.
  • Compiler-generated members appear too: property accessors (get_Name, with IsSpecialName true), backing fields and lambda closures. Filter them when listing.
  • typeof is cheap; GetType() needs an instance and throws NullReferenceException on null. For a nullable value type, GetType() on a boxed int? reports Int32.
  • Type.Name for generics looks like List`1. Use ToString() or the generic arguments to build readable text.
  • Reflection is slower than direct calls and hides code from the trimmer; cache Type, MethodInfo and PropertyInfo objects instead of looking them up repeatedly.
  • Private members can be read through reflection, but that bypasses design intent. Under Native AOT or trimming they may not exist at all.
  • nameof and typeof are compile-time; prefer them over string names when you know the member.