471,337 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

How to write a .NET attribute which does just like ObsoleteAttribute...

Hi,

In .NET there is attribute named ObsoleteAttribute which is derived from
System.Attribute Class. This attribute when applied to a target (class or
method etc.) gives a compiler warning/error. I know we can write our own
attributes by inheriting the System.Attribute but I want to make it give
compiler warning or error when it is applied on the defined attribute
target.

How can we write such an attributes?

Regards,
Ashok G
Jun 27 '08 #1
4 1735
You can't... ObsoleteAttribute is handled as a special-case by the
compiler, not by anything extensible.

What do you want to check, in particular? There may be other ways of
doing it...

Marc
Jun 27 '08 #2
On Apr 24, 8:26*am, "AshokG" <gw2ks...@hotmail.comwrote:
Hi,

In .NET there is attribute named ObsoleteAttribute which is derived from
System.Attribute Class. This attribute when applied to a target (class or
method etc.) gives a compiler warning/error. I know we can write our own
attributes by inheriting the System.Attribute but I want to *make it give
compiler warning or error when it is applied on the defined attribute
target.

How can we write such an attributes?

Regards,
Ashok G
Hi,

AFAIK there is no way of doing that. The compiler knows a given number
of attributes and have CODED actions for them.
You would need to write a new compiler :)
Jun 27 '08 #3
Hi Ashok,

I know you can designate code as 'deprecated'.
Simply deprecate the attribute and each usage of it
will probally result in a compiler warning.

(not sure 100% because i have not tried this yet)

Let me know if it works!

Chazz
AshokG schreef:
Hi,

In .NET there is attribute named ObsoleteAttribute which is derived from
System.Attribute Class. This attribute when applied to a target (class or
method etc.) gives a compiler warning/error. I know we can write our own
attributes by inheriting the System.Attribute but I want to make it give
compiler warning or error when it is applied on the defined attribute
target.

How can we write such an attributes?

Regards,
Ashok G

Jun 27 '08 #4
I think I can do something with AST (Abstract Syntax Tree) of the .NET
framework. I don't have complete onfo on .NET's AST.

Regards,
Ashok G

"AshokG" <gw******@hotmail.comwrote in message
news:uT**************@TK2MSFTNGP02.phx.gbl...
Hi,

In .NET there is attribute named ObsoleteAttribute which is derived from
System.Attribute Class. This attribute when applied to a target (class or
method etc.) gives a compiler warning/error. I know we can write our own
attributes by inheriting the System.Attribute but I want to make it give
compiler warning or error when it is applied on the defined attribute
target.

How can we write such an attributes?

Regards,
Ashok G

Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by bdinmstig | last post: by
2 posts views Thread by Gabriel Genellina | last post: by
13 posts views Thread by Stumped and Confused | last post: by
12 posts views Thread by Radek Maciaszek | last post: by
2 posts views Thread by Steve James | last post: by
21 posts views Thread by Raj | last post: by
6 posts views Thread by Tony Johansson | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.