Home All Groups Group Topic Archive Search About
Author
14 May 2009 12:42 PM
Armin Zingler
Hi,

is there an Attribute that makes the compiler show a warning/hint whenver
the element that carries the attribute is used? Like the ObsoleteAttribute.

<Warning("bla")> _
sub method
end sub

should cause a warning/hint whereever the method is called.

I didn't find anything here
http://msdn.microsoft.com/en-us/library/39967861.aspx


Armin

Author
14 May 2009 1:08 PM
Patrice
When you say "called" do you mean at runtime ??? Where would you want to
write this warning ?

AFAIK if you want to do that you'll have to use a MSIL rewriter (google for
this, saw some mentioned).
Also MS shoudl have something ouf of the box one day (a contract framework,
can't find it for now, found
http://www.codeproject.com/KB/cs/designbycontract.aspx that looks similar).

You may want to give some more details about what you are trying to do (why
the ObsoleteAttribute doesn't fit your needs ?)

--
Patrice

"Armin Zingler" <az.nospam@freenet.de> a écrit dans le message de groupe de
discussion : #X$#bGJ1JHA.5***@TK2MSFTNGP03.phx.gbl...
Show quoteHide quote
> Hi,
>
> is there an Attribute that makes the compiler show a warning/hint whenver
> the element that carries the attribute is used? Like the
> ObsoleteAttribute.
>
> <Warning("bla")> _
> sub method
> end sub
>
> should cause a warning/hint whereever the method is called.
>
> I didn't find anything here
> http://msdn.microsoft.com/en-us/library/39967861.aspx
>
>
> Armin
>
Author
14 May 2009 1:55 PM
Armin Zingler
Patrice wrote:
> When you say "called" do you mean at runtime ??? Where would you want
> to write this warning ?

No, at compile time. Like the ObsoleteAttribute works.

> AFAIK if you want to do that you'll have to use a MSIL rewriter
> (google for this, saw some mentioned).
> Also MS shoudl have something ouf of the box one day (a contract
> framework, can't find it for now, found
> http://www.codeproject.com/KB/cs/designbycontract.aspx that looks
> similar).
> You may want to give some more details about what you are trying to
> do

I want to get a hint from the compiler like I get with those elements that
carry the ObsoleteAttribute.


> (why the ObsoleteAttribute doesn't fit your needs ?)

Because the element is not obsolete.


Armin
Author
14 May 2009 2:53 PM
Mike
Armin Zingler wrote:
Show quoteHide quote
> Patrice wrote:
>> When you say "called" do you mean at runtime ??? Where would you want
>> to write this warning ?
>
> No, at compile time. Like the ObsoleteAttribute works.
>
>> AFAIK if you want to do that you'll have to use a MSIL rewriter
>> (google for this, saw some mentioned).
>> Also MS shoudl have something ouf of the box one day (a contract
>> framework, can't find it for now, found
>> http://www.codeproject.com/KB/cs/designbycontract.aspx that looks
>> similar).
>> You may want to give some more details about what you are trying to
>> do
>
> I want to get a hint from the compiler like I get with those elements
> that carry the ObsoleteAttribute.
>
>
>> (why the ObsoleteAttribute doesn't fit your needs ?)
>
> Because the element is not obsolete.
>

I just explored this ObsoleteAttribute. Interesting.  I noticed it
only displays the warning iff the method is referenced.

So you want the same behavior at compile time, but not say OBSOLETE in
the warning but your string output?

Does custom attributes help?

    http://msdn.microsoft.com/en-us/library/84c42s56.aspx

--
Author
14 May 2009 3:09 PM
Armin Zingler
Mike wrote:
Show quoteHide quote
> Armin Zingler wrote:
>> Patrice wrote:
>>> When you say "called" do you mean at runtime ??? Where would you
>>> want to write this warning ?
>>
>> No, at compile time. Like the ObsoleteAttribute works.
>>
>>> AFAIK if you want to do that you'll have to use a MSIL rewriter
>>> (google for this, saw some mentioned).
>>> Also MS shoudl have something ouf of the box one day (a contract
>>> framework, can't find it for now, found
>>> http://www.codeproject.com/KB/cs/designbycontract.aspx that looks
>>> similar).
>>> You may want to give some more details about what you are trying to
>>> do
>>
>> I want to get a hint from the compiler like I get with those elements
>> that carry the ObsoleteAttribute.
>>
>>
>>> (why the ObsoleteAttribute doesn't fit your needs ?)
>>
>> Because the element is not obsolete.
>>
>
> I just explored this ObsoleteAttribute. Interesting.  I noticed it
> only displays the warning iff the method is referenced.
>
> So you want the same behavior at compile time, but not say OBSOLETE in
> the warning but your string output?

Yep

> Does custom attributes help?

No, because the compiler is not aware of them. It wouldn't cause a
warning/hint in the warnings window. I don't know any attribute that could
be attached to a custom attribute that makes the compiler handle the custom
attribute like the ObsoleteAttribute.

Armin
Author
14 May 2009 4:35 PM
Mike
Sounds like you looking for defines translations, pragmas and
assertions :-)

--

Armin Zingler wrote:
Show quoteHide quote
> Mike wrote:
>> Armin Zingler wrote:
>>> Patrice wrote:
>>>> When you say "called" do you mean at runtime ??? Where would you
>>>> want to write this warning ?
>>>
>>> No, at compile time. Like the ObsoleteAttribute works.
>>>
>>>> AFAIK if you want to do that you'll have to use a MSIL rewriter
>>>> (google for this, saw some mentioned).
>>>> Also MS shoudl have something ouf of the box one day (a contract
>>>> framework, can't find it for now, found
>>>> http://www.codeproject.com/KB/cs/designbycontract.aspx that looks
>>>> similar).
>>>> You may want to give some more details about what you are trying to
>>>> do
>>>
>>> I want to get a hint from the compiler like I get with those elements
>>> that carry the ObsoleteAttribute.
>>>
>>>
>>>> (why the ObsoleteAttribute doesn't fit your needs ?)
>>>
>>> Because the element is not obsolete.
>>>
>>
>> I just explored this ObsoleteAttribute. Interesting.  I noticed it
>> only displays the warning iff the method is referenced.
>>
>> So you want the same behavior at compile time, but not say OBSOLETE in
>> the warning but your string output?
>
> Yep
>
>> Does custom attributes help?
>
> No, because the compiler is not aware of them. It wouldn't cause a
> warning/hint in the warnings window. I don't know any attribute that
> could be attached to a custom attribute that makes the compiler handle
> the custom attribute like the ObsoleteAttribute.
>
>>    http://msdn.microsoft.com/en-us/library/84c42s56.aspx
Author
14 May 2009 4:20 PM
nak
Hi Armin,

>> (why the ObsoleteAttribute doesn't fit your needs ?)
>
> Because the element is not obsolete.

    Silly question, but why would you make a method accessible if you don't
want it to be used?

Nick.
Author
14 May 2009 6:01 PM
Armin Zingler
nak wrote:
> Hi Armin,
>
>>> (why the ObsoleteAttribute doesn't fit your needs ?)
>>
>> Because the element is not obsolete.
>
>    Silly question, but why would you make a method accessible if you
> don't want it to be used?

Who says I don't want it to be used?  :)

(Background: I've changed the return type of a function from an abstract
type to a more specific type (yes, we never change the signature of
published classes yadda-yadda...). Before, I type casted the return value to
the more specific type everywhere I called the function. Now, type casting
is not required anymore in the calling code. Therefore I wanted to add an
attribute to the function to let the compiler tell me where I can remove the
type casting.

Yes, I could find it out another way but I was curious if I could attach an
attribute to do the same.)

So, more out of interest.



Armin
Author
14 May 2009 9:34 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Armin Zingler" <az.nospam@freenet.de> schrieb:
>>>> (why the ObsoleteAttribute doesn't fit your needs ?)
>>>
>>> Because the element is not obsolete.
>>
>>    Silly question, but why would you make a method accessible if you
>> don't want it to be used?
>
> Who says I don't want it to be used?  :)
>
> (Background: I've changed the return type of a function from an abstract
> type to a more specific type (yes, we never change the signature of
> published classes yadda-yadda...). Before, I type casted the return value
> to
> the more specific type everywhere I called the function. Now, type casting
> is not required anymore in the calling code. Therefore I wanted to add an
> attribute to the function to let the compiler tell me where I can remove
> the type casting.
>
> Yes, I could find it out another way but I was curious if I could attach
> an
> attribute to do the same.)

I don't think that using an attribute/compiler warning is a good idea in
this case, because it depends on the calling context whether or not the
warning makes sense.

Instead, I'd introduce a new member (method or property) returning the more
strict type.  However, I assume that you already know that this is the
preferred way to change/extend behavior of an interface which has already
been published.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
14 May 2009 9:47 PM
Armin Zingler
Herfried K. Wagner [MVP] wrote:
>> Yes, I could find it out another way but I was curious if I could
>> attach an
>> attribute to do the same.)
>
> I don't think that using an attribute/compiler warning is a good idea
> in this case, because it depends on the calling context whether or
> not the warning makes sense.
>
> Instead, I'd introduce a new member (method or property) returning
> the more strict type.

If I hadn't already changed the type and didn't remember where I've added
type casting before, I wouldn't have asked if such an attributed exists. :-)
Just curious. Not intended to be used in general.


Armin
Author
15 May 2009 11:03 AM
Patrice
So this is just a way to find out where something is called ? You could
perhaps just use "Find references" in VS or do a file search. It seems a one
shot search/replace to me.

You could use the obsolete attribute just the time needed to check those
changes.

Else I would create a new function and would obsolete the previous one
(would be perhaps my personal preference if this is something beyond just a
search/replacement).


--
Patrice


"Armin Zingler" <az.nospam@freenet.de> a écrit dans le message de groupe de
discussion : OWe3aKM1JHA.2***@TK2MSFTNGP04.phx.gbl...
Show quoteHide quote
> nak wrote:
>> Hi Armin,
>>
>>>> (why the ObsoleteAttribute doesn't fit your needs ?)
>>>
>>> Because the element is not obsolete.
>>
>>    Silly question, but why would you make a method accessible if you
>> don't want it to be used?
>
> Who says I don't want it to be used?  :)
>
> (Background: I've changed the return type of a function from an abstract
> type to a more specific type (yes, we never change the signature of
> published classes yadda-yadda...). Before, I type casted the return value
> to
> the more specific type everywhere I called the function. Now, type casting
> is not required anymore in the calling code. Therefore I wanted to add an
> attribute to the function to let the compiler tell me where I can remove
> the type casting.
>
> Yes, I could find it out another way but I was curious if I could attach
> an
> attribute to do the same.)
>
> So, more out of interest.
>
>
>
> Armin
Author
15 May 2009 3:53 PM
nak
> So this is just a way to find out where something is called ? You could
> perhaps just use "Find references" in VS or do a file search. It seems a
> one shot search/replace to me.

I was just thinking the same.  This seems like a rather drastic method of
debugging code imo.

I would just do it the hard way and locate all references manually then
change them accordingly.

Nick.
Author
15 May 2009 4:28 PM
Mike
nak wrote:
>> So this is just a way to find out where something is called ? You
>> could perhaps just use "Find references" in VS or do a file search. It
>> seems a one shot search/replace to me.
>
> I was just thinking the same.  This seems like a rather drastic method
> of debugging code imo.
>
> I would just do it the hard way and locate all references manually then
> change them accordingly.

I think what he wants is basically in already in C/C++ and C#, more
compiler directives for whatever purpose one needs and there a many
reasons for them.

In C#, there are these two:

#warning message
#error message

--
Author
15 May 2009 4:43 PM
nak
Hi Mike,

> #warning message
> #error message

Sure, but just temporarily in order to fix issues?  Forgive me if I'm wrong
but I thought that was what Armin said.

Nick.
Author
15 May 2009 5:27 PM
Armin Zingler
Guys, you're making it much more complicated than necessary. Don't care
about the bracketed background information I gave. I've done it another way
long ago. It was just the trigger why the question I asked came into my
mind. It was a spontaneous thought, and as I didn't find such an attribute,
maybe useful for other purposes as well (or better), I asked here.
Author
17 May 2009 4:55 PM
Mike
Armin Zingler wrote:
> ... It was a spontaneous thought, and as I didn't find such an attribute,
> maybe useful for other purposes as well (or better), I asked here.
>

Armin, you are probably aware of the <Conditional(CONST or DEFINE)>
attribute but I found this interesting.  :-)

    <Conditional("TEST")> _
    Private Sub Test()
       ...
    End Sub

The main purpose is to satisfy the compiler but it will never called
the function come run time.

This is useful to place test() in various spots throughout your code:

     ...
     ...
     Test()
     ...
     ...
     Test()

and the compiler will only link in the test() method if the TEST
constant is defined.  This is great because it saves one from doing
(like me <g>)

     ...
     ...
#if TEST then
     Test()
#end if
     ...
     ...
#if TEST then
     Test()
#end if

In C/C++ this can done with a #define macro translation

#ifdef TEST
# define Test()
#endif

which the compiler will replace all occurrences of Test() calls with
nothing thus nullifying the statement.

What would be useful if the condition allows a GetType to see if a
class exist.

Anyway, thought I would share this for anyone who didn't know about it.

--
Author
14 May 2009 4:06 PM
Tom Shelton
On 2009-05-14, Armin Zingler <az.nospam@freenet.de> wrote:
Show quoteHide quote
> Hi,
>
> is there an Attribute that makes the compiler show a warning/hint whenver
> the element that carries the attribute is used? Like the ObsoleteAttribute.
>
><Warning("bla")> _
> sub method
> end sub
>
> should cause a warning/hint whereever the method is called.
>
> I didn't find anything here
> http://msdn.microsoft.com/en-us/library/39967861.aspx
>
>
> Armin
>

Armin,

I don't believe that you can create custom compile time attributes...  It
would be nice though :)

--
Tom Shelton