Recall & Review
beginner
What is reflection in C#?
Reflection is a feature in C# that allows a program to inspect and manipulate its own structure and behavior at runtime, such as accessing types, methods, and properties dynamically.
Click to reveal answer
intermediate
How does reflection bypass compile-time safety?
Reflection bypasses compile-time safety by allowing code to access and invoke members dynamically without the compiler checking types or access modifiers, which can lead to runtime errors if used incorrectly.
Click to reveal answer
intermediate
Give an example of a compile-time safety check that reflection can bypass.
An example is accessing a private field or method. Normally, the compiler prevents this, but reflection can access private members at runtime, ignoring access modifiers.
Click to reveal answer
intermediate
What risks come with using reflection to bypass compile-time safety?
Using reflection to bypass compile-time safety can cause runtime errors, security risks, and harder-to-maintain code because type checks and access rules are ignored until runtime.
Click to reveal answer
beginner
Why might a developer choose to use reflection despite bypassing compile-time safety?
Developers use reflection for flexibility, such as dynamically loading types, accessing private members for testing, or building frameworks that work with unknown types at runtime.
Click to reveal answer
What does reflection allow you to do in C#?
✗ Incorrect
Reflection lets you inspect and modify program structure during runtime, not at compile time.
Which safety feature does reflection bypass?
✗ Incorrect
Reflection bypasses compile-time safety checks by allowing dynamic access to members.
What can happen if reflection is used incorrectly?
✗ Incorrect
Incorrect use of reflection can cause runtime errors and security issues.
Which member can reflection access that normal code cannot?
✗ Incorrect
Reflection can access private fields and methods, bypassing access modifiers.
Why is reflection useful despite its risks?
✗ Incorrect
Reflection allows dynamic code that can work with types unknown at compile time.
Explain how reflection can bypass compile-time safety in C# and what that means for program behavior.
Think about what compile-time safety normally protects and how reflection ignores those protections.
You got /4 concepts.
Describe a scenario where using reflection to bypass compile-time safety might be helpful and what precautions should be taken.
Consider why flexibility might be needed despite risks.
You got /4 concepts.