0
0
C Sharp (C#)programming~5 mins

How reflection bypasses compile-time safety in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Reflection lets a program look at and change its own parts while running. This can skip the usual checks done before running, called compile-time safety.

When you want to create or use objects without knowing their types before running the program.
When you need to call methods or access properties dynamically based on user input or configuration.
When building tools that inspect or modify other programs or libraries.
When testing private parts of a class that are not accessible normally.
When loading plugins or modules that are not known at compile time.
Syntax
C Sharp (C#)
Type type = typeof(SomeClass);
MethodInfo method = type.GetMethod("MethodName");
object instance = Activator.CreateInstance(type);
method.Invoke(instance, new object[] { parameters });

Reflection uses types like Type, MethodInfo, and PropertyInfo to inspect and invoke code.

Because it works at runtime, it can bypass compile-time checks like type safety and access modifiers.

Examples
This example calls the ToUpper method on a string using reflection.
C Sharp (C#)
Type type = typeof(string);
MethodInfo method = type.GetMethod("ToUpper", Type.EmptyTypes);
string text = "hello";
string result = (string)method.Invoke(text, null);
Console.WriteLine(result);
This example gets the current date and time by accessing the static Now property of DateTime using reflection.
C Sharp (C#)
Type type = Type.GetType("System.DateTime");
object now = type.GetProperty("Now").GetValue(null);
Console.WriteLine(now);
This example reads a private field value using reflection, which is normally not accessible.
C Sharp (C#)
using System;
using System.Reflection;

class Person {
  private string secret = "hidden";
}

Person p = new Person();
var field = typeof(Person).GetField("secret", BindingFlags.NonPublic | BindingFlags.Instance);
string secretValue = (string)field.GetValue(p);
Console.WriteLine(secretValue);
Sample Program

This program uses reflection to call the ToLower method on a string. It bypasses compile-time checks by invoking the method at runtime.

C Sharp (C#)
using System;
using System.Reflection;

class Program {
    static void Main() {
        Type type = typeof(string);
        MethodInfo method = type.GetMethod("ToLower", Type.EmptyTypes);
        string text = "HELLO WORLD";
        string result = (string)method.Invoke(text, null);
        Console.WriteLine(result);
    }
}
OutputSuccess
Important Notes

Reflection can cause errors at runtime if method names or types are wrong, since compile-time checks are skipped.

Using reflection can slow down your program because it does extra work at runtime.

Reflection can access private members, so use it carefully to avoid breaking encapsulation.

Summary

Reflection lets programs inspect and change themselves while running.

It bypasses compile-time safety checks like type checking and access control.

This is useful for dynamic behavior but should be used carefully to avoid errors and performance issues.