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
You want to apply an attribute to other attributes of the method your attribute is attached to?
I mean like this:
[MyCustomAttribute]
public string ClassAttribute { get; }
And MyCustomAttribute should ensure that the ClassAttribute returns a specific value
You can't really "ensure" that
But is it possible to do that?
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 attributeYes, 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 returnedso like the attribute 'overrides' the property?
Yes
unfortunately you can't do that
attributes are merely metadata, they don't affect behavior or execution in any way
Ok so we call those properties
Because "attributes" already have a very specific definition in c#
Thinker already said that whoops
yeah
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
where?
Oh sorry, settled it wasn't what it looked like