Creating attribute with the same sence as ObsoleteAttribute has  
Author Message
Thecentury





PostPosted: Visual C# Language, Creating attribute with the same sence as ObsoleteAttribute has Top

Hello!

I want to create my own attribute (for example, TODOAttribute), which has to do the following:
if it is assigned to some class, method etc like this:
[TODO("Some planned work/changes in the following class")]
class SomeUnfinishedClass{ }
it should during (after) the BUILD (not in runtime) write a message or warning to ErrorList or Console of Visual Studio with the text which I created this attribute with: on that example I want to see message "Some planned work/changes in the following class" in ErrorList.
I want to use this attribute to remark to myself that I have not finished designing of some class or method.

I tried to do this attribute in a such way:
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public sealed class TODOAttribute : Attribute {
string comment;

public TODOAttribute() { }

public TODOAttribute(string comment) {
this.comment = comment;
StreamWriter sr = new StreamWriter("log.txt", true);
TextWriterTraceListener tr = new TextWriterTraceListener(sr);
Trace.Listeners.Add(tr);
ConsoleTraceListener l = new ConsoleTraceListener();
ConsoleTraceListener e = new ConsoleTraceListener(true);
Trace.Listeners.Add(l);
Trace.Listeners.Add(e);
Trace.WriteLine(comment);
Trace.TraceWarning(comment);
Trace.TraceError(comment);
}
}

You can see that there are many attempts to write in different places: to file "log.txt" or to console, but no of them are working.

Moreover, when I assign this attribute to some method or class and then place tracepoint to the beginning of constructors, I see that this constructor does not work at all, and code inside of it is not performed.

How can I create such kind of attribute
In what moment of time constructor of some attribute works
What shall I read about my problem

Anticipatorily thank you for your help!


Visual C#18  
 
 
Peter Ritchie





PostPosted: Visual C# Language, Creating attribute with the same sence as ObsoleteAttribute has Top

You can't do what you want just with an attribute. You'll have to write some sort of extension for Visual Studio to process the assembly after build and add items to the error list, you can't do that directly from the attribute class.

 
 
Thecentury





PostPosted: Visual C# Language, Creating attribute with the same sence as ObsoleteAttribute has Top

Thank you, Peter, for your answer.

Don't you know, how is it done in case of ObsoleteAttribute
What should I read to learn how to write extensions for VS

When does constructor of Attribute work
I ask this, because I noticed, that code situated inside of attribute constructor does not work at all.

 
 
Peter Ritchie





PostPosted: Visual C# Language, Creating attribute with the same sence as ObsoleteAttribute has Top

The compiler specifically looks for the Obsolete attribute and spits out warnings when found.

If you're looking for simply a TODO sort of task list, if you show the Task List (View/Task List), it shows a list of comments it finds that start with TODO (like "//TODO fix this" or // TODO: fix that"). By default it also looks for comments that start with "HACK" and "UNDONE" and sticks them in the task list. What it searches for and their priority is configurable in the Tools/Options in the Environment/Task List tree entry.

 
 
Thecentury





PostPosted: Visual C# Language, Creating attribute with the same sence as ObsoleteAttribute has Top

Thank you very much!

I was many times said that there is task list in VS, but only you helped me to find it!)

And last question: how can I create extension for VS What shall I read about this theme