-1

I want to do something like this:

[Notes("Remember to blah blah blah")]
public class Foo {

  [Notes("Redo this to include blah blah")]
  public string Bar { get; set; }

  // etc.

}

I know about the ConditionalAttribute, but it's sealed so I can't subclass my NotesAttribute from it.

Can this be done?

h bob
  • 3,610
  • 3
  • 35
  • 51
  • 1
    Could you not simply write a attributes that does nothing? It's obviously just for your own docuumentatio, I'll write //TODO: but if you want to have it with attributes, ok. Now what should your attribute do? nothing? then you have it :) – Florian Burel Aug 14 '16 at 12:31
  • Normally I would use a system like TFS to keep track of work to do... This seems like as bad idea. – Peter Bons Aug 14 '16 at 12:32
  • @PeterBons We don't use TFS. This seems like a good idea for us. – h bob Aug 14 '16 at 12:33
  • @FlorianBurel Yes a no-op attribute is a valid option. But I want to know if the above can be done somehow. BTW this is better than comments in our case because we can reflect over the codebase and run reports. We already use a no-op attribute, but I want to try strip it out of the release build. – h bob Aug 14 '16 at 12:34
  • Unexplained downvote, nice. – h bob Aug 14 '16 at 14:40
  • @FlorianBurel Found a cool way to do this, see my answer. Thanks for your advice. – h bob Aug 17 '16 at 07:22

3 Answers3

1

A possible solution is to use preprocessors:

https://msdn.microsoft.com/en-us/library/4y6tbswk.aspx

#if DEBUG
[Notes("Remember to blah blah blah")]
#endif
public class Foo {

    [Notes("Redo this to include blah blah")]
    public string Bar { get; set; }

    // etc.

}
Peter Bons
  • 26,826
  • 4
  • 50
  • 74
  • 1
    @hbob it sure is :-) But to completely strip it out from release code there are not that many options. At least not that I know of :-P – Peter Bons Aug 14 '16 at 12:44
  • Found a nice way to do this!! See my answer. Thanks for your help. – h bob Aug 17 '16 at 07:22
1

Yes this can be done!

See my related question and answer here.

You need to do this:

[Conditional("DEBUG")]
public class NotesAttribute : Attribute { }
Community
  • 1
  • 1
h bob
  • 3,610
  • 3
  • 35
  • 51
0

You can get the source code of ConditionalAttribute from here:

http://referencesource.microsoft.com/#mscorlib/system/diagnostics/conditionalattribute.cs

You then just copy the code and rename the class and you've got your attribute.

cramopy
  • 3,459
  • 6
  • 28
  • 42
  • I don't think it's that easy. The compiler know to specifically deal with `ConditionalAttribute` in a special way. The attribute's source doesn't do anything special, so it must be a compiler thing. – h bob Aug 14 '16 at 12:37