Sign in to follow this  
BTownTKD

[.net] Can I enforce a specific method signature using a custom Attribute?

Recommended Posts

BTownTKD    205
Topic says it all. I'd like to do some stuff with Methods in .NET, but I also require that any method tagged with my custom Attribute also have a specific signature (for instance null MyMethod() - no parameters, no return type.)


I can do this by throwing an exception at runtime, but it would be so much nicer to throw a compile-time error. Is there a way to enforce this at compile-time?

Share this post


Link to post
Share on other sites
turnpast    1011
As far as I know you cannot use custom attributes to influence compile time behavior. Still this would be awfully nice, I have wanted to do the very same thing on several occasions.

Share this post


Link to post
Share on other sites
Telastyn    3777
I imagine there might be a way to fake this behavior using code templates (use the template to generate no-op/whitespace if good, some compile error on bad). Or a simple post-build task to do the static analysis.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this