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

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Reflection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use reflection in C#?

Reflection lets a program look at its own parts while running. Why might this be useful?

ATo find out the names and types of properties in an object at runtime
BTo permanently change the source code of a program
CTo speed up the program by avoiding method calls
DTo make the program run without any errors
Attempts:
2 left
💡 Hint

Think about when you want to learn about an object without knowing its details before running the program.

Predict Output
intermediate
2:00remaining
Output of reflection code

What will this C# code print?

C Sharp (C#)
using System;
using System.Reflection;

class Person {
    public string Name { get; set; } = "Alice";
    public int Age { get; set; } = 30;
}

class Program {
    static void Main() {
        Person p = new Person();
        Type t = p.GetType();
        foreach (PropertyInfo prop in t.GetProperties()) {
            Console.WriteLine($"{prop.Name}: {prop.GetValue(p)}");
        }
    }
}
A
Name: Alice
Age: 30
BCompilation error
C
Name: Alice
Age: 0
D
Name: 
Age: 0
Attempts:
2 left
💡 Hint

Look at how reflection gets property names and values from the object.

🔧 Debug
advanced
2:00remaining
Why does this reflection code throw an exception?

What error does this code cause and why?

C Sharp (C#)
using System;
using System.Reflection;

class Program {
    static void Main() {
        Type t = Type.GetType("NonExistentClass");
        Console.WriteLine(t.Name);
    }
}
ANo error, prints 'NonExistentClass'
BTypeLoadException because the class does not exist
CCompilation error because NonExistentClass is undefined
DNullReferenceException because Type.GetType returned null
Attempts:
2 left
💡 Hint

What happens if Type.GetType cannot find the class?

📝 Syntax
advanced
2:00remaining
Correct way to get method info using reflection

Which option correctly gets the MethodInfo for a method named 'Calculate' in class 'MathOps'?

C Sharp (C#)
class MathOps {
    public int Calculate(int x) { return x * 2; }
}
AMathOps.GetMethod(Calculate)
BMathOps.GetMethod("Calculate")
Ctypeof(MathOps).GetMethod("Calculate")
Dtypeof(MathOps).GetMethod(Calculate)
Attempts:
2 left
💡 Hint

Remember to use typeof and pass the method name as a string.

🚀 Application
expert
3:00remaining
Using reflection to invoke a method dynamically

Given this code, what is the output?

C Sharp (C#)
using System;
using System.Reflection;

class Greeter {
    public void SayHello(string name) {
        Console.WriteLine($"Hello, {name}!");
    }
}

class Program {
    static void Main() {
        Greeter g = new Greeter();
        MethodInfo mi = typeof(Greeter).GetMethod("SayHello");
        mi.Invoke(g, new object[] { "Bob" });
    }
}
AHello, name!
BHello, Bob!
CRuntime error: TargetParameterCountException
DCompilation error
Attempts:
2 left
💡 Hint

Check how Invoke passes parameters as an object array.