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

Attribute targets and usage in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Attribute targets and usage
Define Attribute
Specify Target(s)
Apply Attribute to Target
Compile and Use Reflection
Access Attribute Info
This flow shows how to define an attribute, specify where it can be used, apply it, then access it at runtime.
Execution Sample
C Sharp (C#)
using System;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
class MyInfoAttribute : Attribute { }

[MyInfo]
class Sample {
  [MyInfo] public void Test() { }
}
Defines an attribute usable on classes and methods, then applies it to a class and a method.
Execution Table
StepActionTargetAttribute AppliedResult
1Define MyInfoAttributeN/AN/AAttribute class created
2Specify AttributeUsageClass, MethodMyInfoAttributeLimits usage to classes and methods
3Apply [MyInfo] to class SampleClass SampleMyInfoAttributeAttribute attached to class
4Apply [MyInfo] to method TestMethod TestMyInfoAttributeAttribute attached to method
5Compile and run reflectionSample classMyInfoAttributeDetects attribute on class and method
6ExitN/AN/AExecution complete
💡 All attribute targets applied and detected, execution ends
Variable Tracker
VariableStartAfter Step 2After Step 4Final
MyInfoAttributeundefineddefined with targets Class, Methodapplied to Sample and Testavailable via reflection
Key Moments - 2 Insights
Why do we specify AttributeTargets in AttributeUsage?
Specifying AttributeTargets restricts where the attribute can be applied, as shown in step 2 of the execution_table. Without it, the attribute could be used anywhere, which may cause errors.
Can we apply the attribute to a property if AttributeTargets only includes Class and Method?
No. The attribute usage limits in step 2 mean applying it to a property would cause a compile error. This is why step 3 and 4 only show class and method targets.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the attribute usage restricted to classes and methods?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Specify AttributeUsage' action in the execution_table at step 2
According to variable_tracker, what is the state of MyInfoAttribute after step 4?
AUndefined
BDefined but not applied
CApplied to Sample and Test
DRemoved
💡 Hint
Look at the 'After Step 4' column for MyInfoAttribute in variable_tracker
If we tried to apply [MyInfo] to a property, what would happen based on the execution flow?
AIt would cause a compile error
BIt would compile and run fine
CIt would be ignored at runtime
DIt would apply only to methods
💡 Hint
Refer to the AttributeTargets restriction in step 2 of execution_table
Concept Snapshot
Define an attribute class by inheriting Attribute.
Use [AttributeUsage] to specify valid targets (e.g., Class, Method).
Apply attribute with [YourAttribute] on allowed targets.
At runtime, use reflection to check attribute presence.
Restricting targets prevents incorrect usage and compile errors.
Full Transcript
This example shows how to create a custom attribute in C# and control where it can be applied using AttributeTargets. First, we define the attribute class MyInfoAttribute. Then, we specify that it can only be used on classes and methods with AttributeUsage. Next, we apply the attribute to a class named Sample and its method Test. Finally, at runtime, reflection can detect these attributes. This prevents applying the attribute to unsupported targets like properties, which would cause compile errors.