0

For reasons I need to read the value of the TargetName-Macro from within my code. It has to be a compile time constant. I have taken a look at Reflection.Assembly, but Reflection does not seem to help me as my module is neither the executing assembly, nor would I get a compile time constant from reflection.

Is there any way I can semi-dynamically, e.g. as a pre-built step, use ProjectSettings-Macros in my code later on?

christian.s
  • 437
  • 6
  • 17
  • Conditional-compilation should be avoided at all costs as it makes the code very hard to maintain. If you must do t though, then set up different Build Configurations, not just Debug and Release and have these set different Conditional Compilation Symbols. You can then test for these symbols in code (via `#if Symbol` pre-compilation syntax) and set `TargetName-Macro` accordingly. – David Arno Jan 08 '16 at 08:17
  • @DavidArno I am not talking about conditional compilation, i need to know the name of my assembly, and this is obviously a compile time constant. all i want is to have this information available in my code. – christian.s Jan 08 '16 at 09:35
  • 1
    Ah, apologies, I misunderstood your question. The way I've tended to tackle that sort of thing is via a per-build event, eg `echo namespace X { static class AssemblyName { const string Name = ""$(TargetName)""; } } > AssemblyName.cs` and then reference that file in your code. – David Arno Jan 08 '16 at 10:02
  • @DavidArno this would be a workaround and not what i thought of, but it will do for now. thank you! – christian.s Jan 08 '16 at 10:17
  • @DavidArno if you post this as an answer i will accept it. when using echo multiple lines and appending with ">>", i get a result i find acceptable. – christian.s Jan 08 '16 at 13:27

1 Answers1

0

When your code is running your module is the executing assembly, so in this case it would help you.

It is not your entry assembly but that's another thing.

The target name is the Assembly name, which is actually your executing assembly file name without its extension.

In fact, you can use CodeBase property of your executing assembly and use Path library to get its file name without extension.

Use this:

Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase)
Ran
  • 747
  • 6
  • 8