❔ CSharp [] attribute help

Hi! sorry for the basic question, but I need a not so technical and more casual explanation about (I think it is) the call of attributes through brackets [] Like this code:
[ApiController]// What is this? What attribute does he call? where from?
[Route("[controller]")]

public class WeatherForecastController : ControllerBase
{
[ApiController]// What is this? What attribute does he call? where from?
[Route("[controller]")]

public class WeatherForecastController : ControllerBase
{
5 Replies
Angius
Angius2y ago
Those are attributes, they don't call anything They're just metadata that other code can use
peladodocinero
peladodocineroOP2y ago
Ah Thanks you
mtreit
mtreit2y ago
While I generally agree with this, we have a ton of code in some attributes we define that gets executed via the attribute constructor and calls all sorts of other code to set up a bunch of data structures, etc. This is invoked when GetCustomAttribute is used to retrieve the attribute metadata. So depending on the code base, there can be a lot of code lurking inside attributes...it's just not executed until someone uses reflection to retrieve the attribute on something.
Angius
Angius2y ago
I might've not been clear enough, yeah, I agree. What I meant is that just adding an attribute to a thing doesn't do anything on its own. Attributes can contain code, but something else needs to call that code.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server