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

How reflection bypasses compile-time safety in C Sharp (C#) - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AGetType()
Bclass
CMyClass
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetType() inside typeof, which is invalid syntax.
Using 'class' keyword instead of a class name.
2fill in blank
medium

Complete the code to get the MethodInfo object for the method named "Calculate".

C Sharp (C#)
MethodInfo method = type.GetMethod([1]);
Drag options to blanks, or click blank then click option'
ACalculate()
B"Calculate"
CCalculate
DmethodName
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the method name without quotes.
Including parentheses in the method name string.
3fill in blank
hard

Fix the error in invoking the method using reflection.

C Sharp (C#)
object result = method.Invoke([1], null);
Drag options to blanks, or click blank then click option'
Anull
Btype
Cmethod
Dthis
Attempts:
3 left
💡 Hint
Common Mistakes
Passing null for instance methods causes runtime errors.
Passing the Type object instead of an instance.
4fill in blank
hard

Fill both blanks to create a dictionary of method names and their return types using reflection.

C Sharp (C#)
var methods = type.GetMethods();
var dict = new Dictionary<string, Type> { { [1], [2] } };
Drag options to blanks, or click blank then click option'
Amethods[0].Name
Bmethods[0].ReturnType
Cmethods[1].Name
Dmethods[1].ReturnType
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing method indices for key and value.
Using method names for both key and value.
5fill in blank
hard

Fill all three blanks to filter methods with no parameters and create a dictionary of their names and return types.

C Sharp (C#)
var filtered = methods.Where(m => m.GetParameters().Length [1] 0);
var dict = filtered.ToDictionary(m => m.[2], m => m.[3]);
Drag options to blanks, or click blank then click option'
A==
BName
CReturnType
DLength
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect comparison operators like > or <.
Using Length property instead of Name or ReturnType.