Challenge - 5 Problems
Reflection Attribute Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of reading a custom attribute on a class
What is the output of this C# program that reads a custom attribute from a class using reflection?
C Sharp (C#)
using System; [AttributeUsage(AttributeTargets.Class)] public class InfoAttribute : Attribute { public string Description { get; } public InfoAttribute(string description) => Description = description; } [Info("Sample class for testing")] public class SampleClass {} public class Program { public static void Main() { var type = typeof(SampleClass); var attr = (InfoAttribute)Attribute.GetCustomAttribute(type, typeof(InfoAttribute)); Console.WriteLine(attr?.Description ?? "No attribute found"); } }
Attempts:
2 left
💡 Hint
Look at how the attribute is applied and how reflection retrieves it.
✗ Incorrect
The class SampleClass has the InfoAttribute with Description set to "Sample class for testing". Reflection reads this attribute and prints its Description property.
❓ Predict Output
intermediate2:00remaining
Output when attribute is missing
What will be the output when trying to read a custom attribute that is not applied to the class?
C Sharp (C#)
using System; [AttributeUsage(AttributeTargets.Class)] public class InfoAttribute : Attribute { public string Description { get; } public InfoAttribute(string description) => Description = description; } public class NoAttributeClass {} public class Program { public static void Main() { var type = typeof(NoAttributeClass); var attr = (InfoAttribute)Attribute.GetCustomAttribute(type, typeof(InfoAttribute)); Console.WriteLine(attr?.Description ?? "No attribute found"); } }
Attempts:
2 left
💡 Hint
Check what happens when the attribute is not present and null is returned.
✗ Incorrect
Since NoAttributeClass does not have the InfoAttribute, GetCustomAttribute returns null, so the null-coalescing operator prints "No attribute found".
🔧 Debug
advanced2:30remaining
Identify the error when reading multiple attributes
This code tries to read multiple custom attributes from a class but throws an exception. What is the cause?
C Sharp (C#)
using System; using System.Reflection; [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class TagAttribute : Attribute { public string Name { get; } public TagAttribute(string name) => Name = name; } [Tag("alpha")] [Tag("beta")] public class MultiTagClass {} public class Program { public static void Main() { var type = typeof(MultiTagClass); var attr = (TagAttribute)Attribute.GetCustomAttributes(type, typeof(TagAttribute)); Console.WriteLine(attr.Name); } }
Attempts:
2 left
💡 Hint
Check the return type of GetCustomAttributes when multiple attributes are allowed.
✗ Incorrect
Attribute.GetCustomAttributes returns an array of matching attributes. Since AllowMultiple = true and two attributes are applied, it returns TagAttribute[], but the code casts directly to TagAttribute, causing InvalidCastException.
📝 Syntax
advanced2:30remaining
Correct syntax to read all attributes of a type
Which option correctly reads all custom attributes of type TagAttribute applied to a class?
C Sharp (C#)
using System; using System.Reflection; [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class TagAttribute : Attribute { public string Name { get; } public TagAttribute(string name) => Name = name; } [Tag("one")] [Tag("two")] public class TestClass {} public class Program { public static void Main() { var type = typeof(TestClass); // Which option correctly reads all TagAttribute instances? } }
Attempts:
2 left
💡 Hint
GetCustomAttributes returns an array of attributes, not a single attribute.
✗ Incorrect
GetCustomAttributes returns an array of attributes matching the type. Casting it to TagAttribute[] is correct. Option D returns a single attribute, not all. Option D is invalid because GetCustomAttribute is not a method on Type. Option D tries to cast an array to a single attribute causing an error.
🚀 Application
expert3:00remaining
Count how many properties have a specific attribute
Given a class with properties decorated with a custom attribute, what is the value of count after running this code?
C Sharp (C#)
using System; using System.Reflection; [AttributeUsage(AttributeTargets.Property)] public class ImportantAttribute : Attribute {} public class Data { [Important] public int Id { get; set; } public string Name { get; set; } [Important] public double Value { get; set; } } public class Program { public static void Main() { var type = typeof(Data); int count = 0; foreach (var prop in type.GetProperties()) { if (Attribute.IsDefined(prop, typeof(ImportantAttribute))) count++; } Console.WriteLine(count); } }
Attempts:
2 left
💡 Hint
Count only properties with the ImportantAttribute applied.
✗ Incorrect
Only the properties Id and Value have the ImportantAttribute. Name does not. So the count is 2.