Recall & Review
beginner
What is an attribute in C#?
An attribute is a way to add extra information (metadata) to code elements like classes, methods, or properties. It helps describe or control behavior without changing the code logic.
Click to reveal answer
beginner
How do you read attributes using reflection in C#?
You use reflection to get the type or member info, then call methods like GetCustomAttributes() to find the attributes applied to that element.
Click to reveal answer
intermediate
What does the method GetCustomAttributes() return?
It returns an array of objects representing all attributes applied to a member or type. You can then check or cast these objects to specific attribute types.
Click to reveal answer
intermediate
Why use reflection to read attributes?
Reflection lets you inspect code metadata at runtime, so you can change behavior dynamically based on attributes without hardcoding logic.
Click to reveal answer
beginner
Show a simple example of reading a custom attribute from a class using reflection.Define a custom attribute class, apply it to a class, then use typeof(MyClass).GetCustomAttributes() to get and read the attribute data.
Click to reveal answer
Which method is commonly used to get attributes of a class in C#?
✗ Incorrect
GetCustomAttributes() returns all attributes applied to a class or member.
What type of object does GetCustomAttributes() return?
✗ Incorrect
It returns an array containing all attribute objects applied.
Why would you use reflection to read attributes?
✗ Incorrect
Reflection allows runtime inspection of metadata like attributes.
Which namespace must you include to use reflection in C#?
✗ Incorrect
System.Reflection contains classes for reflection operations.
If you want to check if a class has a specific attribute, which approach is best?
✗ Incorrect
GetCustomAttributes() lets you find if a specific attribute is applied.
Explain how to read custom attributes from a class using reflection in C#.
Think about how to get metadata at runtime and identify the attribute.
You got /5 concepts.
Why is reading attributes with reflection useful in programming?
Consider how metadata can guide program decisions without changing code.
You got /4 concepts.