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?
ASystem.PropertyInfo
BSystem.MethodInfo
CSystem.Reflection
DSystem.Type
✗ Incorrect
System.Type represents the type and provides methods like GetMethods() and GetProperties() to inspect members.
What does the GetMethods() method return?
AAn array of MethodInfo objects
BAn array of PropertyInfo objects
CA list of strings with method names
DA boolean indicating if methods exist
✗ Incorrect
GetMethods() returns MethodInfo objects that describe each method.
How do you get the name of a property using reflection?
AUse GetPropertyName() method
BUse the Name property of PropertyInfo
CUse ToString() on MethodInfo
DCall GetName() on Type
✗ 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?
AReturnType
BMethodType
CType
DResultType
✗ Incorrect
ReturnType gives the type of value the method returns.
Why is inspecting methods and properties useful?
ATo prevent errors at compile time
BTo speed up program execution
CTo write code that works with unknown types at runtime
DTo change method code dynamically
✗ 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.
Practice
(1/5)
1. Which C# method is used to get the type information of an object instance?
easy
A. typeof()
B. GetMethods()
C. GetProperties()
D. GetType()
Solution
Step 1: Understand the difference between GetType() and typeof()
GetType() is called on an object instance to get its runtime type, while typeof() is used with a type name at compile time.
Step 2: Identify the method that works on an object instance
Since the question asks for the method used on an object instance, GetType() is the correct choice.
Final Answer:
GetType() -> Option D
Quick Check:
Object instance method for type info = GetType() [OK]
Hint: Use GetType() on objects, typeof() on type names [OK]
Common Mistakes:
Confusing typeof() with GetType()
Using GetMethods() to get type info
Thinking GetProperties() returns type
2. Which of the following is the correct syntax to get all methods of the String type in C#?
easy
A. typeof(String).GetMethods()
B. typeof(String).GetProperties()
C. String.GetMethods()
D. GetType(String).GetMethods()
Solution
Step 1: Recall how to get methods from a type
Use typeof(TypeName).GetMethods() to get all methods of a type.
Step 2: Check each option's syntax
typeof(String).GetMethods() uses typeof(String).GetMethods(), which is correct. typeof(String).GetProperties() calls GetProperties(), not methods. String.GetMethods() tries to call GetMethods() on the type name directly, which is invalid. GetType(String).GetMethods() uses GetType(String), which is incorrect syntax.
Final Answer:
typeof(String).GetMethods() -> Option A
Quick Check:
Get methods from type = typeof(Type).GetMethods() [OK]
Hint: Use typeof(Type).GetMethods() to list methods [OK]
Common Mistakes:
Calling GetMethods() on type name directly
Mixing GetProperties() with GetMethods()
Using GetType() with a type name instead of instance
3. What will the following C# code output?
var props = typeof(DateTime).GetProperties();
Console.WriteLine(props.Length);
medium
A. 0
B. More than 0
C. Compilation error
D. Runtime exception
Solution
Step 1: Understand what GetProperties() returns
GetProperties() returns an array of all public properties of the type.
Step 2: Check if DateTime has properties
DateTime has many properties like Now, Today, Year, Month, etc., so the array length will be more than zero.
Final Answer:
More than 0 -> Option B
Quick Check:
DateTime properties count > 0 [OK]
Hint: Types like DateTime have many properties, so length > 0 [OK]
Common Mistakes:
Assuming no properties means length 0
Expecting compilation or runtime errors
Confusing methods with properties
4. Identify the error in this C# code snippet:
var typeInfo = "hello".GetType();
var methods = typeInfo.GetMethod();
medium
A. GetType() is invalid on string
B. Missing parentheses on GetType
C. GetMethod() should be GetMethods()
D. Cannot call GetMethod() on typeInfo
Solution
Step 1: Check usage of GetType()
"hello".GetType() is valid and returns the type of the string instance.
Step 2: Check method name on typeInfo
There is no method called GetMethod() without parameters; to get all methods, use GetMethods().
Final Answer:
GetMethod() should be GetMethods() -> Option C
Quick Check:
Use GetMethods() to get all methods [OK]
Hint: Use GetMethods() (plural) to list all methods [OK]
Common Mistakes:
Using GetMethod() without parameters
Thinking GetType() is invalid on string
Missing parentheses on method calls
5. You want to list all public properties of a custom class Person and print their names. Which code snippet correctly does this?
hard
A. var props = typeof(Person).GetProperties(); foreach(var p in props) Console.WriteLine(p.Name);
B. var props = new Person().GetProperties(); foreach(var p in props) Console.WriteLine(p.Name);
C. var props = typeof(Person).GetMethods(); foreach(var p in props) Console.WriteLine(p.Name);
D. var props = Person.GetProperties(); foreach(var p in props) Console.WriteLine(p.Name);
Solution
Step 1: Identify how to get properties of a type
Use typeof(Person).GetProperties() to get all public properties of the Person class.
Step 2: Check the foreach loop to print property names
Iterate over the properties and print each p.Name to display property names.
Step 3: Verify other options
var props = new Person().GetProperties(); foreach(var p in props) Console.WriteLine(p.Name); tries to call GetProperties() on an instance, which is invalid. var props = typeof(Person).GetMethods(); foreach(var p in props) Console.WriteLine(p.Name); gets methods, not properties. var props = Person.GetProperties(); foreach(var p in props) Console.WriteLine(p.Name); calls GetProperties() directly on the class name, which is invalid syntax.
Final Answer:
var props = typeof(Person).GetProperties(); foreach(var p in props) Console.WriteLine(p.Name); -> Option A
Quick Check:
Use typeof(Type).GetProperties() and loop to print names [OK]
Hint: Use typeof(Class).GetProperties() and loop to print names [OK]
Common Mistakes:
Calling GetProperties() on an instance
Using GetMethods() instead of GetProperties()
Calling GetProperties() directly on class name
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.