C
C#2y ago
Suiram1

Set a class attribute result with a custom attribute

Hi, I'm trying to create a custom attribute that applies to class attributes of type string and ensures that when you read the value of this class attribute, it always returns a specific string. Is this somehow feasible?
14 Replies
Althos
Althos2y ago
You want to apply an attribute to other attributes of the method your attribute is attached to?
Suiram1
Suiram1OP2y ago
I mean like this: [MyCustomAttribute] public string ClassAttribute { get; } And MyCustomAttribute should ensure that the ClassAttribute returns a specific value
Thinker
Thinker2y ago
You can't really "ensure" that
Suiram1
Suiram1OP2y ago
But is it possible to do that?
Thinker
Thinker2y ago
Can you elaborate further on how you want this to work? Like should the attribute limit the values of the property or what? Also public string Foo { get; } is a property, not an attribute
Suiram1
Suiram1OP2y ago
Yes, I mean a property and I would like that if you read the property, i.e. string i = MyProperty, that a value that is stored in the attribute is returned
Thinker
Thinker2y ago
so like the attribute 'overrides' the property?
Suiram1
Suiram1OP2y ago
Yes
Thinker
Thinker2y ago
unfortunately you can't do that attributes are merely metadata, they don't affect behavior or execution in any way
ero
ero2y ago
Ok so we call those properties Because "attributes" already have a very specific definition in c# Thinker already said that whoops
Thinker
Thinker2y ago
[Attribute]
public int Property { get; }
[Attribute]
public int Property { get; }
yeah
Suiram1
Suiram1OP2y ago
Ok, thanks I just thought it could be done because I saw something like that in other c# code where I thought it would do this
Thinker
Thinker2y ago
where?
Suiram1
Suiram1OP2y ago
Oh sorry, settled it wasn't what it looked like
Want results from more Discord servers?
Add your server