第一步是让Csmacro.exe走上正轨。
第二步,打开需要条件编译的项目,添加预编译事件:Csmacro.exe $(project dir)。
第三步,编辑源文件,例如将Patch.cs文件修改为:
1: #region include 'Csmacro_Template.cs' 2: #endregion 3: 4: #if NET2 5: 6: namespace System.Runtime.CompilerServices 7: {8: public class ExtensionAttribute : Attribute { } 9: }10: 11: namespace System12: {13: public delegate void Action();14: public delegate void ActionT0,T1(T0 t0,T1 t1);15: }16: 17: #endif
#region include是我介绍的Csmacro的宏语法。详见C#模板编程(二):写C#预处理器,让模板自然来。点击编译,
系统将生成一个包含以下内容的Patch_Csmacro.cs文件:
1: #define NET2 2: 3: #if NET2 4: 5: namespace System.Runtime.CompilerServices 6: { 7: public class ExtensionAttribute : Attribute { } 8: } 9: 10: namespace System11: {12: public delegate void Action();13: public delegate void ActionT0,T1(T0 t0,T1 t1);14: }15: 16: #endif
第四步,将生成的Patch_Csmacro.cs添加到项目中。
完成后选择不同的目标,编译时生成的就是目标的条件编译!