I have a custom attribute that I would like to limit to methods with a return type of void.
I know that I can restrict methods using [AttributeUsage(AttributeTargets.Method)] , but there seems to be no way to limit the return type or any other aspect of the method signature.
The [System.Diagnostics.Conditional] attribute has exactly the limitation that I want. Adding it to a non-empty method results in a compiler error:
The conditional attribute is not valid in '(SomeMethod)' because its return type is not void
and IntelliSense says:
The 'System.Diagnostics.ConditionalAttribute' attribute is valid only for attribute classes or methods with a return type of 'void'.
If I am F12 in ConditionalAttribute , I see that it is decorated with the following attributes:
[Serializable]
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
[ComVisible (true)]
Nothing is said about the return type.
How do I do this for the Conditional attribute and can I do the same for my custom attribute?
c # attributes custom-attributes postsharp
Roman reiner
source share