문제

I'm trying to figure out if there's any way to avoid getting an "Unreachable code" warning for something that's caused by the preprocessor. I don't want to suppress all such warnings, only those which will be dependent on the preprocessor, e.g.

#if WINDOWS
    public const GamePlatform platform = GamePlatform.PC;
#else
    public const GamePlatform platform = GamePlatform.MAC;
#endif

And later on there's code that goes:

if (platform == GamePlatform.PC)
{
    ...
}
else
{
    ...
}

One of those two sections will always be detected as "Unreachable code", and we've got those all over the place. I'd like to try and get rid of the many warnings it creates, but I still want to get warnings for legitimately unreachable code. (In reality, there's more than just two platforms, so each chunk of platform-specific code creates a pile of unnecessary warnings.)

도움이 되었습니까?

해결책

Option 1: Add the preprocessor macros wherever you have the if-statements. This will be more performant, but perhaps a bit uglier.

Option 2: Make the platform variable not const. Setting it to static readonly made the warning go away for me.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top