0Pricing
C# Academy · Lesson

Understanding Reflection in C#

Use reflection to inspect and manipulate objects at runtime.

Understanding Reflection in C# is a free C# Academy lesson on CoddyKit — lesson 5 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Understanding Reflection in C#

Welcome to the next lesson! In this lesson, you’ll learn about reflection in C#, a powerful feature that allows you to inspect and interact with types and objects at runtime. Let’s get started!

Understanding Reflection in C# — illustration 1

2

What is Reflection?

Reflection is the ability of a program to inspect and manipulate its own structure at runtime. It is commonly used for:

  • Accessing metadata about types, methods, and properties.
  • Dynamically invoking methods and accessing fields.
  • Building frameworks and tools like serializers and ORMs.

Key Point: Reflection is part of the System.Reflection namespace.

3

Accessing Type Information

Use the Type class to access metadata about a type. Example:

Tip: Use GetProperties, GetMethods, and GetFields to inspect type members.

using System;

class SampleClass {
    public int Id { get; set; }
    public string Name { get; set; }
}

class Program {
    static void Main() {
        Type type = typeof(SampleClass);

        Console.WriteLine("Type Name: " + type.Name);
        Console.WriteLine("Properties:");
        foreach (var prop in type.GetProperties()) {
            Console.WriteLine("- " + prop.Name);
        }
    }
}

4

Dynamically Creating Objects

You can use reflection to create instances of a class dynamically. Example:

Key Point: Use Activator.CreateInstance to create objects at runtime.

using System;

class SampleClass {
    public string Name { get; set; }
}

class Program {
    static void Main() {
        Type type = typeof(SampleClass);
        object instance = Activator.CreateInstance(type);

        var property = type.GetProperty("Name");
        property.SetValue(instance, "Reflection Example");

        Console.WriteLine("Name: " + property.GetValue(instance));
    }
}

5

Invoking Methods Dynamically

Reflection allows you to call methods at runtime without knowing them at compile time. Example:

Tip: Use GetMethod and Invoke for dynamic method calls.

using System;

class SampleClass {
    public void Greet(string name) {
        Console.WriteLine("Hello, " + name);
    }
}

class Program {
    static void Main() {
        Type type = typeof(SampleClass);
        object instance = Activator.CreateInstance(type);

        var method = type.GetMethod("Greet");
        method.Invoke(instance, new object[] { "World" });
    }
}

6

Working with Attributes

Reflection can be used to inspect custom attributes applied to types, methods, or properties. Example:

Key Point: Use GetCustomAttributes to retrieve attribute information.

using System;

[AttributeUsage(AttributeTargets.Class)]
class SampleAttribute : Attribute {
    public string Description { get; }
    public SampleAttribute(string description) {
        Description = description;
    }
}

[Sample("This is a sample class.")]
class SampleClass {}

class Program {
    static void Main() {
        Type type = typeof(SampleClass);
        var attributes = type.GetCustomAttributes(typeof(SampleAttribute), false);

        foreach (SampleAttribute attr in attributes) {
            Console.WriteLine("Description: " + attr.Description);
        }
    }
}

7

Practice Challenge

Create a program that uses reflection to inspect a class, list its methods and properties, and invoke one of its methods dynamically. Example:

using System;

class SampleClass {
    public int Add(int a, int b) {
        return a + b;
    }
}

class Program {
    static void Main() {
        Type type = typeof(SampleClass);
        Console.WriteLine("Methods:");
        foreach (var method in type.GetMethods()) {
            Console.WriteLine("- " + method.Name);
        }

        object instance = Activator.CreateInstance(type);
        var addMethod = type.GetMethod("Add");
        int result = (int)addMethod.Invoke(instance, new object[] { 5, 3 });

        Console.WriteLine("Result of Add: " + result);
    }
}

9

10

Best Practices for Using Reflection

Here are some best practices:

  • Use reflection sparingly, as it can impact performance.
  • Cache reflection results to improve efficiency in repeated calls.
  • Prefer compile-time solutions over reflection when possible.
  • Use reflection for dynamic scenarios like plugins and frameworks.

Tip: Combine reflection with custom attributes for flexible and dynamic applications.

11

Great Job!

Congratulations! You’ve learned how to use reflection to inspect and manipulate types at runtime in C#. This concludes the "Advanced C# Features" category. Let’s move forward to more coding adventures!

Understanding Reflection in C# — illustration 10

Frequently asked questions

Is the “Understanding Reflection in C#” lesson free?

Yes — the full text of “Understanding Reflection in C#” is free to read here on the web, and the C# Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Understanding Reflection in C#”?

Use reflection to inspect and manipulate objects at runtime. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 5 of 5, so you can start here or from the beginning and move at your own pace.

How long does the “Understanding Reflection in C#” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Generics in C#
  2. Async and Await: Working with Asynchronous Code
  3. Understanding Threading and Tasks
  4. Exploring Delegates and Lambdas
  5. Understanding Reflection in C#
← Back to C# Academy