Overview
The System.Type
class represents type declarations: class types, interface types, array types, value types, enumeration types, type parameters, generic type definitions, and open or closed constructed generic types.
Syntax
Remarks
Inheritance
public abstract class Type : MemberInfo, IReflect, _Type, RuntimeType
Use typeof(T)
or obj.GetType()
to obtain a Type
instance. The Type
class provides methods to inspect metadata, create instances, and manipulate generic types at runtime.
Object
└─MemberInfo
└─Type
Methods
Signature | Description |
---|---|
public object? GetConstructor(Type[]? types) | Gets a constructor that matches the specified argument types. |
public MethodInfo? GetMethod(string name, BindingFlags bindingAttr) | Retrieves a method by name with the specified binding flags. |
public PropertyInfo? GetProperty(string name) | Gets a public property with the specified name. |
public bool IsAssignableFrom(Type c) | Determines whether an instance of the current type can be assigned from an instance of the specified type. |
public Type[] GetInterfaces() | Returns all the interfaces implemented or inherited by the current type. |
public object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args) | Invokes the specified member, using the specified binding constraints. |
public static Type? GetTypeFromHandle(RuntimeTypeHandle handle) | Gets the type represented by the specified type handle. |
Properties
Name | Type | Description |
---|---|---|
Assembly | Assembly | The assembly in which the type is defined. |
BaseType | Type? | The type from which the current type directly inherits. |
FullName | string? | The fully qualified name of the type, including its namespace. |
IsClass | bool | Indicates whether the type is a class. |
IsEnum | bool | Indicates whether the type is an enumeration. |
IsInterface | bool | Indicates whether the type is an interface. |
IsGenericType | bool | True if the type is a generic type definition or constructed generic type. |
Namespace | string? | The namespace of the type. |
Examples
// Get the Type object for System.String
Type stringType = typeof(string);
// List all public methods of System.String
foreach (MethodInfo method in stringType.GetMethods())
{
Console.WriteLine($"{method.ReturnType.Name} {method.Name}");
}
// Check if a type implements IEnumerable
bool implementsIEnumerable = typeof(List<int>).GetInterfaces()
.Any(i => i == typeof(IEnumerable));
// Create an instance of a type using its parameterless constructor
object? obj = Activator.CreateInstance(typeof(DateTime));
Console.WriteLine(obj?.GetType().FullName);