4

Is there a recommended Attribute in the .Net Framework for marking code as "Not supported"?

So far I have been using ObsoleteAttribute but it's not always entirely accurate. For example at the moment I am writing a managed FMOD wrapper and I want to clearly mark properties, enum values etc which only apply to unsupported platforms.

Say I have something like an enum for determining the format of an audio file. I want to make it clear that, for the .Net port at least, certain values are not relevant. For example:

enum SoundFormat{
    Wav,
    Mp3,
    OggVorbis,

    [Obsolete("Xbox360 only")] Xma,
    [Obsolete("GameCube only")] GcadPcm,
    [Obsolete("PS2/PSP only")] Vag,
}

While using Obsolete works and kind of serves the same purpose, it's also misleading as they're not technically obsolete by being unsupported. It's one of those things which are ultimately of little consequence but it keeps bugging me every time I put Obsolete where it's inaccurate.

PS: before it's suggested, NotImplementedException and NotSupportedException really do not answer the question as they only provide run-time guidance. I'm more interested in compile-time.

nathanchere
  • 8,008
  • 15
  • 65
  • 86

2 Answers2

7

I would suggest you to use compiler directives to create different versions of assembly:

enum SoundFormat
{
    Wav,
    Mp3,
    OggVorbis,
 #if Xbox360        
    Xma,
 #elif GameCube        
    GcadPcm,
 #elif PS
    Vag,
 #endif
}

If you will compile assembly only with Xbox360 defined, then enum will look like:

enum SoundFormat
{
    Wav,
    Mp3,
    OggVorbis,     
    Xma,
}

UPDATE: It was not clear from your original question what you are trying to achieve. But if you want all these enum members to exist, and have compiler warnings if platform-specific format used, then you can use approach suggested by Pablo Fernandez:

[Obsolete("Not supported")]
public class NotSupportedAttribute : Attribute
{
}

Then decorate platform-specific formats with NotSupported attribute (which describes intent better than Obsolete):

enum SoundFormat
{
    Wav,
    Mp3,
    OggVorbis,
    [NotSupported]     
    Xma,
    [NotSupported]
    GcadPcm,
    [NotSupported]
    Vag
}

Compiler will still generate 'obsolete' warning, but with specific message:

Warning 1 'Foo.Bar.NotSupportedAttribute' is obsolete: 'Not supported'

Community
  • 1
  • 1
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • There are no different versions. There is simply no support at all offered for Xbox, PS2 etc. I'm only including the extra enums etc for completeness since they are part of the underlying DLL's API, and in case for example, a non-managed function returns a result with a value which I've excluded and casting to enum will throw an exception etc. – nathanchere Apr 09 '14 at 10:07
  • Plus in the example you provided, Xma GcadPcm and Vag will all = 3. – nathanchere Apr 09 '14 at 10:10
  • @nathanchere in my example only one of members (Xma = 3) will exist in enum. There will not be three members with value 3. Also your first comment is completely not clear to me. You are asking for compile-time guidance, but saying about runtime value returned by non-managed function. Please add more explanation of what you are trying to achieve. Some code may be helpful – Sergey Berezovskiy Apr 09 '14 at 10:38
  • That's still just using `ObsoleteAttribute`, except it's worse because now instead of the current warning messages like "Obsolete: Xbox only" they're all the same message "Obsolete: Not supported". I was looking for an alternative to Obsolete. – nathanchere Apr 09 '14 at 12:47
  • From MSDN: "ObsoleteAttribute is applicable to all program elements except assemblies, modules, parameters, and return values. Marking an element as obsolete informs users that the element will be removed in future versions of the product." Not supported != deprecated or intended for removal. I appreciate the effort but I think the answer is what I'm after just isn't possible. – nathanchere Apr 09 '14 at 12:48
0

It looks like the answer is: no, there is nothing better than marking as [Obsolete].

nathanchere
  • 8,008
  • 15
  • 65
  • 86