Fallback Script For Conditional Compilation

Hello,

I have an enum StatusInfo which I’m creating through Code Generation, and using in a couple of other scripts.

The issue is that, when the StatusInfo enum doesn’t exist, the scripts which depend on it will fail to compile.

So what I want is to somehow create a fallback.cs file (or something), that checks if statusinfo.cs exists. If it does, fallback.cs should be ignored for compilation (or else there’ll be the issue of two classes with the same name), but if statusinfo.cs doesn’t exist fallback.cs should be compiled (to avoid compiler errors for missing definitions).

Does anyone know of any way I can accomplish this?
Thanks in advance

1 Answer

1

You can solve this in many ways :

  • You can create a Compilation Define Symbol that you call CODE_GEN_FALLBACK for example, then in your fallback script you can wrap it like this
#if CODE_GEN_FALLBACK
// todo : define your class here
#endif
  • You can compile the scripts in your project using the roslyn API and check the AST manually to see if your auto-generated script exists , if yes then everything is fine and you can delete any fallback script if it exists, else (first : keep the fallback script in txt file) just recreate a .cs file where you paste the script text into it

alright thanks. I think this is super useful

–