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

Inspecting methods and properties in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ASystem.PropertyInfo
BSystem.MethodInfo
CSystem.Reflection
DSystem.Type
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
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
Which property of MethodInfo tells you the return type of a method?
AReturnType
BMethodType
CType
DResultType
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
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.