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

Why reflection is needed in C Sharp (C#) - Test Your Understanding

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 object.

C Sharp (C#)
Type type = obj.[1]();
Drag options to blanks, or click blank then click option'
AGetObjectType
BGetClass
CGetType
DTypeOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using TypeOf which is a keyword, not a method.
Using GetClass which does not exist in C#.
2fill in blank
medium

Complete the code to get the name of the class using reflection.

C Sharp (C#)
string className = obj.GetType().[1];
Drag options to blanks, or click blank then click option'
ATypeName
BFullName
CClassName
DName
Attempts:
3 left
💡 Hint
Common Mistakes
Using FullName which returns namespace plus class name.
Using ClassName or TypeName which do not exist.
3fill in blank
hard

Fix the error in the code to get all public properties of a class.

C Sharp (C#)
PropertyInfo[] properties = obj.GetType().[1]();
Drag options to blanks, or click blank then click option'
AGetProperties
BGetMembers
CGetFields
DGetAttributes
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetFields which returns fields, not properties.
Using GetMembers which returns all members, not just properties.
4fill in blank
hard

Fill both blanks to invoke a method named 'Print' with no parameters using reflection.

C Sharp (C#)
MethodInfo method = obj.GetType().[1]("Print");
method.[2](obj, null);
Drag options to blanks, or click blank then click option'
AGetMethod
BInvoke
CCallMethod
DExecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using CallMethod or Execute which do not exist.
Passing parameters incorrectly to Invoke.
5fill in blank
hard

Fill all three blanks to create an instance of a class named 'Person' using reflection.

C Sharp (C#)
Type type = Type.[1]("Person");
object obj = Activator.[2](type);
string name = (string)type.GetProperty("Name").[3](obj);
Drag options to blanks, or click blank then click option'
AGetType
BCreateInstance
CGetValue
DFindType
Attempts:
3 left
💡 Hint
Common Mistakes
Using FindType which does not exist.
Using wrong method to create instance or get property value.