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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AshokG

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

    Hi,

    In .NET there is attribute named ObsoleteAttribu te which is derived from
    System.Attribut e 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.Attribut e 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


  • Marc Gravell

    #2
    Re: How to write a .NET attribute which does just likeObsoleteAtt ribute...

    You can't... ObsoleteAttribu te 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

    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: How to write a .NET attribute which does just likeObsoleteAtt ribute...

      On Apr 24, 8:26 am, "AshokG" <gw2ks...@hotma il.comwrote:
      Hi,
      >
      In .NET there is attribute named ObsoleteAttribu te which is derived from
      System.Attribut e 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.Attribut e 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 :)

      Comment

      • Chazz

        #4
        Re: How to write a .NET attribute which does just like ObsoleteAttribu te...

        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 ObsoleteAttribu te which is derived from
        System.Attribut e 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.Attribut e 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
        >
        >

        Comment

        • AshokG

          #5
          Re: How to write a .NET attribute which does just like ObsoleteAttribu te...

          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" <gw2ksoft@hotma il.comwrote in message
          news:uTeKFZgpIH A.1236@TK2MSFTN GP02.phx.gbl...
          Hi,
          >
          In .NET there is attribute named ObsoleteAttribu te which is derived from
          System.Attribut e 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.Attribut e 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
          >

          Comment

          Working...