I'm thinking you can't but is there a way to reference a method as a parameter on an attribute? I.e. something like below? I can fall back to use strings, but prefer to use compiler time to verify the types are correct.
[LinkToAction(Something)]
public void SomethingElse()
{
}
public static void Something()
{
}
public class LinkToActionAttribute : Attribute
{
public LinkToActionAttribute(MethodInfo info)
{
}
}
A method is a function defined in the class. An attribute is an instance variable defined in the class.
Attributes are used in C# to convey declarative information or metadata about various code elements such as methods, assemblies, properties, types, etc. Attributes are added to the code by using a declarative tag that is placed using square brackets ([ ]) on top of the required code element.
An annotation on a program element (commonly a class, method, or field) is a piece of meta-data added to that program element which can be used to embellish that element with extra code. In Java this is called an annotation, in C# this is called an attribute.
Sorry, but you can't. Just these can be passed as arguments for attributes:
This question is similar to yours: Is it possible to have a delegate as attribute parameter?. There, a workaround is available which can be useful to you.
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