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

Reading attributes with reflection in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Reflection Attribute Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
    }
}
ASample class for testing
BNo attribute found
CSystem.InfoAttribute
DCompilation error
Attempts:
2 left
💡 Hint
Look at how the attribute is applied and how reflection retrieves it.
Predict Output
intermediate
2: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");
    }
}
AEmpty line
Bnull
CNo attribute found
DCompilation error
Attempts:
2 left
💡 Hint
Check what happens when the attribute is not present and null is returned.
🔧 Debug
advanced
2: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);
    }
}
AInvalid cast exception because GetCustomAttributes returns an array
BOutputs 'alpha'
CCompilation error due to missing using directive
DNullReferenceException because no attribute found
Attempts:
2 left
💡 Hint
Check the return type of GetCustomAttributes when multiple attributes are allowed.
📝 Syntax
advanced
2: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?
    }
}
Avar attrs = Attribute.GetCustomAttribute(type, typeof(TagAttribute));
Bvar attrs = (TagAttribute)Attribute.GetCustomAttributes(type, typeof(TagAttribute));
Cvar attrs = type.GetCustomAttribute(typeof(TagAttribute));
Dvar attrs = (TagAttribute[])Attribute.GetCustomAttributes(type, typeof(TagAttribute));
Attempts:
2 left
💡 Hint
GetCustomAttributes returns an array of attributes, not a single attribute.
🚀 Application
expert
3: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);
    }
}
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
Count only properties with the ImportantAttribute applied.