Recall & Review
beginner
What is reflection in C#?
Reflection is a way to look at the details of types, methods, and properties in a program while it is running. It helps you find out what methods and properties a class has.Click to reveal answer
beginner
How do you get all methods of a class using reflection?You use the
GetMethods() method on a Type object to get an array of all methods in that class.Click to reveal answer
beginner
What does
GetProperties() return?It returns an array of <code>PropertyInfo</code> objects, each representing a property of the class you are inspecting.Click to reveal answer
intermediate
How can you find out the return type of a method using reflection?
Each
MethodInfo object has a ReturnType property that tells you the type of value the method returns.Click to reveal answer
intermediate
Why might you want to inspect methods and properties at runtime?
Inspecting methods and properties lets you write flexible code that can work with objects you don’t know about until the program runs. For example, plugins or tools that work with many types.
Click to reveal answer
Which C# class is used to get information about methods and properties of a type?
✗ Incorrect
System.Type represents the type and provides methods like GetMethods() and GetProperties() to inspect members.
What does the
GetMethods() method return?✗ Incorrect
GetMethods() returns MethodInfo objects that describe each method.How do you get the name of a property using reflection?
✗ Incorrect
PropertyInfo objects have a Name property that gives the property’s name.
Which property of MethodInfo tells you the return type of a method?
✗ Incorrect
ReturnType gives the type of value the method returns.
Why is inspecting methods and properties useful?
✗ Incorrect
Reflection helps write flexible code that can handle types discovered only when the program runs.
Explain how you can use reflection to list all methods and properties of a class in C#.
Think about how to get the Type first, then how to get members from it.
You got /5 concepts.
Describe a real-life scenario where inspecting methods and properties at runtime would be helpful.
Consider situations where you don’t know all types before running the program.
You got /4 concepts.