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

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Reflection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of modifying private field using reflection
What is the output of this C# code that uses reflection to change a private field value?
C Sharp (C#)
using System;
using System.Reflection;

class Person {
    private string name = "Alice";
    public void PrintName() {
        Console.WriteLine(name);
    }
}

class Program {
    static void Main() {
        Person p = new Person();
        p.PrintName();
        FieldInfo fi = typeof(Person).GetField("name", BindingFlags.NonPublic | BindingFlags.Instance);
        fi.SetValue(p, "Bob");
        p.PrintName();
    }
}
AAlice\nBob
BAlice\nAlice
CCompilation error due to private field access
DRuntime exception: FieldInfo is null
Attempts:
2 left
💡 Hint
Reflection can access private members at runtime, bypassing compile-time restrictions.
🧠 Conceptual
intermediate
1:30remaining
Why reflection bypasses compile-time safety
Why does using reflection in C# allow code to bypass compile-time safety checks?
ABecause reflection modifies the source code before compilation
BBecause reflection only works with public members, so safety is not bypassed
CBecause reflection disables the compiler's type checking permanently
DBecause reflection works at runtime and can access metadata and members regardless of access modifiers
Attempts:
2 left
💡 Hint
Think about when reflection operates and what it can access.
🔧 Debug
advanced
2:00remaining
Identify the runtime error caused by reflection misuse
What runtime error will this code produce when trying to set a non-existent field using reflection?
C Sharp (C#)
using System;
using System.Reflection;

class Sample {
    private int value = 10;
}

class Program {
    static void Main() {
        Sample s = new Sample();
        FieldInfo fi = typeof(Sample).GetField("nonExistentField", BindingFlags.NonPublic | BindingFlags.Instance);
        fi.SetValue(s, 20);
    }
}
AArgumentException
BNullReferenceException
CFieldAccessException
DNo exception, sets value to 20
Attempts:
2 left
💡 Hint
What happens if GetField returns null and you call SetValue on it?
Predict Output
advanced
2:00remaining
Output of invoking private method via reflection
What will this C# program print when it invokes a private method using reflection?
C Sharp (C#)
using System;
using System.Reflection;

class Calculator {
    private int Double(int x) {
        return x * 2;
    }
}

class Program {
    static void Main() {
        Calculator calc = new Calculator();
        MethodInfo mi = typeof(Calculator).GetMethod("Double", BindingFlags.NonPublic | BindingFlags.Instance);
        int result = (int)mi.Invoke(calc, new object[] { 5 });
        Console.WriteLine(result);
    }
}
A10
B5
CRuntime exception: MethodInfo is null
DCompilation error due to private method access
Attempts:
2 left
💡 Hint
Reflection can invoke private methods at runtime.
🧠 Conceptual
expert
2:30remaining
Security risks of reflection bypassing compile-time safety
Which of the following is a major security risk caused by reflection bypassing compile-time safety in C#?
ASlower compilation times due to reflection metadata processing
BCompile-time errors that prevent program execution
CUnauthorized access to private data or methods leading to data leaks or manipulation
DInability to use inheritance and polymorphism
Attempts:
2 left
💡 Hint
Think about what private means and what happens if reflection ignores it.