2

How to inject a new code in existing C# code (not MSIL) before compiling, but not change .cs file?

Aleksandr Vishnyakov
  • 1,872
  • 5
  • 23
  • 39

4 Answers4

3

You can use the Text Template Transformation Toolkit (T4) to handle this type of scenario.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
3

Use T4 templates which write to partial class files. This is the pattern used by Visual Studio--auto-generated code goes in partial files, while your code goes in the main .cs file.

2

You may take a look at PostSharp.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

You can using Fody or Weavlyn which working with MSIL injection "IL Weaving"

Osama AbuSitta
  • 3,918
  • 4
  • 35
  • 51