System.Reflection Namespace

Namespace: System.Reflection

Provides types that allow late-bound access and inspection of code running in a .NET environment. This namespace includes types for retrieving information about application modules, the types they contain, and the members (methods, constructors, properties, fields, attributes, etc.) of those types.

Reflection is a powerful tool for inspection and manipulation of types and members at runtime. It is often used in frameworks, development tools, and debugging scenarios.

Types in System.Reflection

Type: Type

Represents type declarations for class libraries, such as class, interface, or value type, or an enumeration, array reference, or boxing created from a given type. The Type object is used to obtain metadata about a type.

Summary

The Type class is the fundamental entry point for reflection. You can obtain a Type object for any type in your application, and then use it to discover information about the type, its members, and its attributes.

Common Operations

  • Getting the name of a type.
  • Getting the base type.
  • Getting the declaring type.
  • Getting the assembly containing the type.
  • Getting the namespace of the type.
  • Listing all members of a type (fields, properties, methods, etc.).
  • Invoking methods or constructors.
  • Creating instances of a type.

Example Usage


using System;

public class Example
{
    public static void Main(string[] args)
    {
        Type stringType = typeof(string);

        Console.WriteLine($"Name: {stringType.Name}");
        Console.WriteLine($"Full Name: {stringType.FullName}");
        Console.WriteLine($"Namespace: {stringType.Namespace}");
        Console.WriteLine($"Is Class: {stringType.IsClass}");

        Console.WriteLine("\nMethods:");
        foreach (var method in stringType.GetMethods())
        {
            Console.WriteLine($"- {method.Name}");
        }
    }
}
                

Members

Member Signature Description
FullName string FullName { get; } Gets the namespace-qualified name of the represented type.
Name string Name { get; } Gets the name of the current type.
BaseType Type BaseType { get; } Gets the Type of the direct base class of the Type.
IsClass bool IsClass { get; } Gets a value indicating whether the current Type is a class.
IsAbstract bool IsAbstract { get; } Gets a value indicating whether the Type is abstract.
GetMethods() MethodInfo[] GetMethods() Searches for the public methods declared by this type.
GetProperties() PropertyInfo[] GetProperties() Searches for the public properties declared by this type.
GetFields() FieldInfo[] GetFields() Searches for the public fields declared by this type.
GetConstructors() ConstructorInfo[] GetConstructors() Searches for the public constructors declared by this type.

Type: MethodInfo

Represents a method on a type, and provides methods for getting information about the method and invoking it.

Summary

MethodInfo objects are obtained from a Type object and represent a specific method. They allow you to inspect the method's return type, parameters, name, and modifiers, and to invoke the method dynamically.

Example Usage


using System;
using System.Reflection;

public class MethodExample
{
    public void Greet(string name)
    {
        Console.WriteLine($"Hello, {name}!");
    }

    public static void Main(string[] args)
    {
        MethodExample instance = new MethodExample();
        Type type = instance.GetType();

        MethodInfo greetMethod = type.GetMethod("Greet");

        if (greetMethod != null)
        {
            object[] parameters = { "World" };
            greetMethod.Invoke(instance, parameters);
        }
    }
}
                

Members

Member Signature Description
Name string Name { get; } Gets the name of the current member.
ReturnType Type ReturnType { get; } Gets the return type of this method.
GetParameters() ParameterInfo[] GetParameters() Gets an array of objects that contain information about the parameters of this method.
Invoke(object obj, object[] parameters) object Invoke(object obj, object[] parameters) Invokes the method reflected by this instance, with the specified parameters on the specified object.