Challenge - 5 Problems
Reflection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of MethodInfo Name Property
What will be the output of this C# code snippet that uses reflection to get method names?
C Sharp (C#)
using System; using System.Reflection; class Sample { public void Hello() { } private int Add(int a, int b) { return a + b; } } class Program { static void Main() { MethodInfo[] methods = typeof(Sample).GetMethods(BindingFlags.Public | BindingFlags.Instance); foreach (var method in methods) { Console.WriteLine(method.Name); } } }
Attempts:
2 left
π‘ Hint
Remember that BindingFlags.Public only includes public methods, so private methods like Add won't appear.
β Incorrect
The code uses BindingFlags.Public and Instance to get only public instance methods. 'Hello' is public, 'Add' is private and excluded. The inherited public methods from Object class also appear.
β Predict Output
intermediate2:00remaining
PropertyInfo GetValue Output
What will be printed by this C# program that uses reflection to get a property value?
C Sharp (C#)
using System; using System.Reflection; class Person { public string Name { get; set; } = "Alice"; } class Program { static void Main() { Person p = new Person(); PropertyInfo prop = typeof(Person).GetProperty("Name"); Console.WriteLine(prop.GetValue(p)); } }
Attempts:
2 left
π‘ Hint
GetValue returns the value of the property for the given object instance.
β Incorrect
The property 'Name' has the value 'Alice' in the instance p, so GetValue(p) returns 'Alice'.
π§ Debug
advanced2:00remaining
Why does this reflection code throw an exception?
This code throws a NullReferenceException. What is the cause?
C Sharp (C#)
using System; using System.Reflection; class Car { private int speed = 100; } class Program { static void Main() { Car c = new Car(); PropertyInfo prop = typeof(Car).GetProperty("speed"); Console.WriteLine(prop.GetValue(c)); } }
Attempts:
2 left
π‘ Hint
Check if 'speed' is a property or a field and how GetProperty works.
β Incorrect
GetProperty only finds properties, not fields. 'speed' is a private field, so GetProperty returns null, causing NullReferenceException when calling GetValue.
π Syntax
advanced2:00remaining
Which code correctly gets all public properties including inherited ones?
Choose the correct C# code snippet that retrieves all public instance properties of a class including those inherited.
Attempts:
2 left
π‘ Hint
Public instance properties include those with public getters/setters and are instance members.
β Incorrect
BindingFlags.Public | BindingFlags.Instance returns all public instance properties including inherited ones. Other options either get non-public or static properties.
π Application
expert3:00remaining
Count properties with public getter and private setter
Given this class, how many properties have a public getter and a private setter?
C Sharp (C#)
using System; class Book { public string Title { get; private set; } public string Author { get; set; } public int Pages { get; private set; } public DateTime Published { get; private set; } private string InternalCode { get; set; } }
Attempts:
2 left
π‘ Hint
Check each propertyβs getter and setter accessibility carefully.
β Incorrect
Title, Pages, and Published have public getters and private setters. Author has public setter, InternalCode is private property.