Challenge - 5 Problems
Reflection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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(); } }
Attempts:
2 left
💡 Hint
Reflection can access private members at runtime, bypassing compile-time restrictions.
✗ Incorrect
The code first prints the original private field value 'Alice'. Then reflection accesses the private field 'name' and sets it to 'Bob'. The second print shows 'Bob'. No compile error or exception occurs because reflection bypasses compile-time safety.
🧠 Conceptual
intermediate1:30remaining
Why reflection bypasses compile-time safety
Why does using reflection in C# allow code to bypass compile-time safety checks?
Attempts:
2 left
💡 Hint
Think about when reflection operates and what it can access.
✗ Incorrect
Reflection operates at runtime and uses metadata to access members, including private ones, ignoring compile-time access restrictions. It does not modify source code or disable compiler checks.
🔧 Debug
advanced2: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); } }
Attempts:
2 left
💡 Hint
What happens if GetField returns null and you call SetValue on it?
✗ Incorrect
GetField returns null if the field does not exist. Calling SetValue on null causes a NullReferenceException at runtime.
❓ Predict Output
advanced2: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); } }
Attempts:
2 left
💡 Hint
Reflection can invoke private methods at runtime.
✗ Incorrect
The private method Double multiplies input by 2. Reflection finds and invokes it with argument 5, returning 10. No compile error or exception occurs.
🧠 Conceptual
expert2: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#?
Attempts:
2 left
💡 Hint
Think about what private means and what happens if reflection ignores it.
✗ Incorrect
Reflection can access private members, which can expose sensitive data or allow unauthorized changes, creating security vulnerabilities. It does not cause compile errors or affect inheritance.