Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"NotSupported" attribute?

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.

like image 892
nathanchere Avatar asked Mar 20 '23 22:03

nathanchere


1 Answers

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'

like image 106
Sergey Berezovskiy Avatar answered Mar 28 '23 16:03

Sergey Berezovskiy