I'd like to create a custom attribute but restrict its usage to interface's methods. Is it possible to do that in C#?
I'd like to have compilator error when I try to do this:
public class SomeClass
{
[MyAttribute]
public void SomeMethod() { ... }
}
[MyAttribute]
public interface IMyInterface { ... }
I want to be be able to do only this:
public interface IMyInterface
{
[MyAttribute]
void SomeMethod();
}
I can limit my attribute to interface or to methods but not both in the same time
[AttributeUsage(AttributeTargets.Method)]
It's not possible. Have a look at the AttributeTargets enum. You can limit it to a method, but not for a method of an interface.
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