Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the type of the class using reflection.
C Sharp (C#)
Type type = typeof([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetType() instead of typeof for a type.
Using attribute or reflection keywords incorrectly.
✗ Incorrect
To get the type of a class, use typeof(ClassName).
2fill in blank
mediumComplete the code to get the custom attributes of the class.
C Sharp (C#)
object[] attrs = type.GetCustomAttributes([1], false); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing true or false instead of a Type object.
Passing null which returns all attributes.
✗ Incorrect
To get specific attributes, pass typeof(AttributeName) to GetCustomAttributes.
3fill in blank
hardFix the error in the code to check if the attribute exists on the class.
C Sharp (C#)
bool hasAttr = type.IsDefined([1], false); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing true or false instead of a Type.
Passing the attribute class name without typeof.
✗ Incorrect
IsDefined expects a Type object for the attribute, so use typeof(MyAttribute).
4fill in blank
hardFill both blanks to get the first custom attribute and cast it to the correct type.
C Sharp (C#)
MyAttribute attr = (MyAttribute)type.GetCustomAttributes([1], false)[[2]];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong index like 1 which may cause errors if only one attribute exists.
Passing false instead of typeof for the attribute type.
✗ Incorrect
GetCustomAttributes returns an array; index 0 is the first attribute. Use typeof to specify attribute type.
5fill in blank
hardFill all three blanks to read a property value from the attribute instance.
C Sharp (C#)
var value = attr.[1]; // Access property if (value [2] [3]) { Console.WriteLine("Property value is valid."); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == causing logic errors.
Using wrong property name that does not exist.
✗ Incorrect
Access the property by name, compare it with the expected string using ==.