0

I have a few C# (*.cs) files in my project that need to be included in compilation as well as distributed as source code along with the application (the application itself is not open-source).

Currently, I have the Build Action for such files set to C# Compiler, and I then use a Post-build event to copy those files to the application directory. This is far from ideal in my scenario since these files are frequently. In any case, the Post-build depend on referencing the VS solution/project path to access the *.cs files.

There seems to be no way to associate a single file with multiple Build Actions. The objective is to have these files compiled and also be available as plain C# text in the app's Resource Manifest.

I am sure many would ask why I want to do this. Suffice it to say, this is a complex code-generation scenario where the app itself consumes these files and then presents them to end-users to consume as a template (plain text C# code). I thought about decompilation but that is overkill and would not preserve code formatting.

Any advice would be appreciated.

UPDATE: A simpler way to understand the objective. I need to edit the the file at compile-time with full solution-wide Intellisense context AND somehow access the same source code at runtime.

6
  • Have you considered extracting that code to a contracts/abstractions assembly so that both your application and the generated code can reference the assembly? Commented Sep 23, 2024 at 3:49
  • @madreflection: Yes, I have considered that. Sadly, my scenario is akin to eat your own dog food where references would have to be circular for this to work. That is also why I have not used ISourceGenerator capabilities. And T4 is a pain due to dev overhead and lack of tooling (even the best third-party T4 editors are a pain). Commented Sep 23, 2024 at 4:28
  • If there are circular dependencies, you just haven't separated things enough yet. Commented Sep 23, 2024 at 4:30
  • If a single file can't work, how about create a soft/hard link to dup the file? Commented Sep 23, 2024 at 5:26
  • @shingo, Could you elaborate please? Commented Sep 23, 2024 at 8:39

1 Answer 1

0

I cannot believe that I missed this!

I was concentrating on the Build Action and did not realize that project items which are set to compile can also be copied to the output directory.

Project item properties

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.