I have a custom attribute and I would like it to have the name of a property as input. Because the name is a string it is a valid input type (as attributes are quite limited as to what they can have as input to their constructors). But how can I accomplish this?
Take this example code:
public class MyCustomAttribute : Attribute {
public MyCustomAttribute(string propertyName) {}
}
public class Foo {
public bool MyCustomProperty { get; set; }
[MyCustom(SomeMagicAppliedToMyCustomProperty)] // I want the attribute to receive something along the lines of "Foo.MyCustomProperty"
public void Bar();
}
How can I accomplish this with the limitations to what an attribute can receive in its constructor?
There's a new feature in c#-6.0 nameof() that gives the name of the particular property, variable, class etc as a string,
http://www.c-sharpcorner.com/UploadFile/7ca517/the-new-feature-of-C-Sharp-6-0-nameof-operator/ https://msdn.microsoft.com/en-us/magazine/dn802602.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With