Sometimes, you want to get extra information about your code while it runs. Reading attributes with reflection lets you do that by checking special notes (attributes) on your code parts.
Reading attributes with reflection in C Sharp (C#)
var type = typeof(YourClass); var attributes = type.GetCustomAttributes(typeof(YourAttribute), inherit: true); foreach (var attr in attributes) { var yourAttr = (YourAttribute)attr; // Use yourAttr properties here }
typeof(YourClass) gets the type information of the class.
GetCustomAttributes returns all attributes of the specified type.
Obsolete attribute message from a class.[Obsolete("Use NewMethod instead")] class OldClass {} var attrs = typeof(OldClass).GetCustomAttributes(typeof(ObsoleteAttribute), true); foreach(var attr in attrs) { var obsolete = (ObsoleteAttribute)attr; Console.WriteLine(obsolete.Message); }
Serializable attribute is present on the class.[Serializable] class Data {} bool isSerializable = typeof(Data).IsDefined(typeof(SerializableAttribute), true); Console.WriteLine(isSerializable);
This program defines a custom attribute InfoAttribute with a description. It adds this attribute to a class and a method. Then it uses reflection to read and print the descriptions.
using System; [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] class InfoAttribute : Attribute { public string Description { get; } public InfoAttribute(string description) => Description = description; } [Info("This is a sample class")] class SampleClass { [Info("This is a sample method")] public void SampleMethod() {} } class Program { static void Main() { var type = typeof(SampleClass); var classAttrs = type.GetCustomAttributes(typeof(InfoAttribute), true); foreach (InfoAttribute attr in classAttrs) { Console.WriteLine($"Class attribute description: {attr.Description}"); } var method = type.GetMethod("SampleMethod"); var methodAttrs = method.GetCustomAttributes(typeof(InfoAttribute), true); foreach (InfoAttribute attr in methodAttrs) { Console.WriteLine($"Method attribute description: {attr.Description}"); } } }
Reflection can slow down your program if used too much, so use it only when needed.
Attributes can be applied to many code parts like classes, methods, properties, and more.
Reflection lets you read extra notes (attributes) added to your code.
You can check attributes on classes, methods, and other parts at runtime.
This helps customize behavior or build flexible tools.
Practice
Solution
Step 1: Understand what attributes are
Attributes are extra notes or metadata added to code elements like classes or methods.Step 2: Understand reflection's role
Reflection lets you look at these attributes while the program runs, to read their information.Final Answer:
Access extra information added to classes or methods at runtime -> Option AQuick Check:
Reflection reads attributes = Access extra info [OK]
- Thinking reflection changes code instead of reading it
- Confusing attributes with variables or methods
- Believing attributes speed up compilation
Solution
Step 1: Recall the reflection syntax for attributes
In C#, to get attributes of a class, use typeof(ClassName).GetCustomAttributes().Step 2: Check each option
Only typeof(MyClass).GetCustomAttributes() uses the correct syntax with typeof and GetCustomAttributes method.Final Answer:
typeof(MyClass).GetCustomAttributes() -> Option BQuick Check:
Correct syntax uses typeof and GetCustomAttributes() [OK]
- Calling GetAttributes() directly on class name
- Using instance methods instead of typeof
- Confusing method names like Attributes()
[System.Obsolete("Use NewMethod instead")]
class OldClass {}
var attrs = typeof(OldClass).GetCustomAttributes(typeof(System.ObsoleteAttribute), false);
Console.WriteLine(attrs.Length);Solution
Step 1: Identify the attribute applied
The class OldClass has the Obsolete attribute applied once.Step 2: Check GetCustomAttributes call
GetCustomAttributes is called with ObsoleteAttribute type, so it returns an array with that attribute instance.Final Answer:
1 -> Option DQuick Check:
One Obsolete attribute found = 1 [OK]
- Assuming zero because attribute is ignored
- Expecting null instead of empty array
- Thinking it throws exception without reason
class MyClass {}
var attrs = MyClass.GetCustomAttributes();
Console.WriteLine(attrs.Length);Solution
Step 1: Check method call on class
GetCustomAttributes is a method of Type, not the class itself, so calling MyClass.GetCustomAttributes() is invalid.Step 2: Correct usage
You must use typeof(MyClass).GetCustomAttributes() to get attributes via reflection.Final Answer:
GetCustomAttributes is not a method of the class itself -> Option CQuick Check:
Use typeof(Class).GetCustomAttributes() not Class.GetCustomAttributes() [OK]
- Calling GetCustomAttributes directly on class name
- Ignoring that GetCustomAttributes is a Type method
- Assuming attributes exist without checking
InfoAttribute applied to multiple methods in a class and print their descriptions. Which approach correctly reads and prints these descriptions?Solution
Step 1: Identify target elements
The attribute InfoAttribute is applied to methods, so we must get methods via reflection.Step 2: Read attributes per method
For each MethodInfo, call GetCustomAttributes with InfoAttribute type to get the attribute instance and access its Description property.Step 3: Print descriptions
Print the Description property for each method's attribute found.Final Answer:
Use reflection to get MethodInfo array, then for each method call GetCustomAttributes(typeof(InfoAttribute), false) and print the Description property -> Option AQuick Check:
Read attributes per method, not class or fields [OK]
- Reading attributes only on class instead of methods
- Checking fields instead of methods
- Assuming one attribute for whole class
Ready to prove it?
Pass the test and this pattern moves unknown
