C
Join ServerC#
help
Trying to learn about attributes - can someone explain this simple one and why it doesnt work?
AAntiMatter1/26/2023
code is pretty self explanatory, why when I set a debug point in the attribute it wont stop? in addition it prints altho the value is 1 and not 0
TestFunc(1);
[ZeroCheck]
void TestFunc(int value)
{
Console.WriteLine("Value is not 0");
}
[AttributeUsage(AttributeTargets.Method)]
public class ZeroCheckAttribute : Attribute
{
public void OnEntry(int value)
{
if (value == 0)
{
throw new Exception("Cannot proceed as value is 0");
}
}
}
EEro1/26/2023
I mean first of all attributes are purely metadata. They don't do anything on their own
EEro1/26/2023
But also, 1 != 0
EEro1/26/2023
So i don't know what you expect it to do
AAntiMatter1/26/2023
I mean, isnt there an [authorize] attribute that checks for auth?
EEro1/26/2023
Which does a bunch of stuff because it was implemented by another class to do a bunch of stuff
AAntiMatter1/26/2023
so it can be done no? lets say i wanted to do something like this whats the way?
EEro1/26/2023
You need to check for the attribute's existence using things like reflection
EEro1/26/2023
No clue, someone else has to answer that
EEro1/26/2023
I never use attributes
AAntiMatter1/26/2023
well feel free to tag one of the c# gods, thanks for explaning a bit mate
EEro1/26/2023
@Orannis
OOrannis1/26/2023
That doesn't work like you seem to think it does
OOrannis1/26/2023
It doesn't change your method
OOrannis1/26/2023
The caller of your method looks for that attribute and enables authorization if it's there
AAntiMatter1/26/2023
but it does prevent the method from being executed if im not authorized, lets say i wanted to accomplish the same thing if the integer is 0 and not 1
AAntiMatter1/26/2023
how would i go about that?
AAntiMatter1/26/2023
lets say something simpler
AAntiMatter1/26/2023
an attribute that prevents a methode from doing a cw
AAntiMatter1/26/2023
if it returns false
OOrannis1/26/2023
Because the caller of your method looks for that attribute
OOrannis1/26/2023
You'd need to do the same
OOrannis1/26/2023
Ie, where you call this method, look for that attribute
AAntiMatter1/26/2023
can u show me a short example?
OOrannis1/26/2023
No short examples exist
OOrannis1/26/2023
Just make a simple helper method to throw when out of range, don't use attributes
AAntiMatter1/26/2023
I know there are other ways, trying to learn about attributes becuse sometimes i see these monster foot long attributes being used in code
AAntiMatter1/26/2023
and im cluelss as to how they work
OOrannis1/26/2023
What we're discussing here is a pattern of coding called Aspect-Oriented (AOP). C# doesn't really support that
OOrannis1/26/2023
Attributes are purely metadata
OOrannis1/26/2023
That's it
OOrannis1/26/2023
They're tags
EEro1/26/2023
When does an attribute's ctor get ran?
AAntiMatter1/26/2023
Buy they [Authorize] One has meaning, even if the caller function looks for it
AAntiMatter1/26/2023
some logic does happen
AAntiMatter1/26/2023
some code is being invoked
OOrannis1/26/2023
Because the caller looks at that metadata
OOrannis1/26/2023
Like when you're searching on an online store, and you use a filter
OOrannis1/26/2023
You looked for a tag, read the data it had, and selected it
OOrannis1/26/2023
And then everything that didn't have the tag was filtered out
OOrannis1/26/2023
Same deal with attributes
AAntiMatter1/26/2023
I see, good explanation, but just to clarify
AAntiMatter1/26/2023
if i call a method that has the auth attribute, the invoker does a meta data check to see if that user lets say is singed in, and if he's not and it returns like a false boolean, the method doesnt get invoked at all
OOrannis1/26/2023
Yup
AAntiMatter1/26/2023
I see, great stuff, thanks mate have a good evening !
OOrannis1/26/2023
The caller uses reflection to get that info
OOrannis1/26/2023
You too