What’s the C# syntax for applying an attribute to a return type ?
[return: SomeAttribute()]
public string GatValue()
{
return"Some Value";
}
Here’s a simple example of a custom attribute used for return types
[AttributeUsage(AttributeTargets.ReturnValue, AllowMultiple = false)]
class MyAttribute : Attribute
{
public string Description { get; set; }
}
public class SomeClass
{
[return: MyAttribute(Description = "A Return Value")]
public string GetValue()
{
return "Hello World";
}
}
class Program
{
static void Main(string[] args)
{
object[] attributes = typeof (SomeClass).
GetMethod("GetValue").
ReturnTypeCustomAttributes.
GetCustomAttributes(false);
}
}
This will return a single item in the object[] of type MyAttribute.