简体   繁体   English

如果指令不存在于 c# 中,我的预处理器指令中的代码是否包含在编译代码中?

[英]Does my code inside preprocessor directives included in compiled code if the directive is not present in c#?

I have an application which I want to create a demo for it.我有一个应用程序,我想为它创建一个演示。 I prefer to give another exe for my paid version and different one for demo to prevent people with demo version to crack my paid version.我更喜欢为我的付费版本提供另一个 exe,为演示提供不同的 exe,以防止使用演示版本的人破解我的付费版本。 For now I have commented the code I need in demo so whenever I have to create a demo I go over these comments and make the necessary changes.现在我已经对演示中需要的代码进行了注释,因此每当我必须创建演示时,我都会查看这些注释并进行必要的更改。 Today I realize the #if directive..今天我意识到#if 指令..

Now my question is if I have a code like this现在我的问题是我是否有这样的代码

#if DEMO
   public string test()
   {
     return "blabla";
   }
#endif

and I did not define DEMO directive, so my code gets greyed out obviously and I can't reach this method which is what I was expecting so far.而且我没有定义DEMO指令,所以我的代码显然变灰了,我无法达到这个方法,这是我目前所期望的。 Now I have rebuild my project without defining DEMO directive and I deleted my .pdb file from output folder after that I tried to check my executable with dotpeek and I could not see this test method there.现在我在没有定义DEMO指令的情况下重建了我的项目,然后我从输出文件夹中删除了我的 .pdb 文件,之后我尝试用 dotpeek 检查我的可执行文件,但我在那里看不到这个测试方法。

Can I use this approach to separate my demo and paid version executables?我可以使用这种方法来分离我的演示版和付费版可执行文件吗? Lets say this test method is a demo method so it wont be seen in paid version if someone cracks it.假设这个test方法是一个演示方法,所以如果有人破解它就不会在付费版本中看到它。 And same goes for opposite, if this test method is a paid version method and I put it inside #if !DEMO then if I define DEMO and rebuild my exe I won't see this test method there right?反之亦然,如果这个test方法是付费版本方法,我把它放在#if !DEMO那么如果我定义DEMO并重建我的 exe,我不会在那里看到这个test方法,对吗?

As I mention before I already checked the code and could not see my methods with dotpeek but I just want to be 100% sure that these methods wont seen in programs like dotpeek or anywhere else正如我之前提到的,我已经检查了代码并且无法使用 dotpeek 看到我的方法,但我只想 100% 确定这些方法不会在 dotpeek 或其他任何程序中看到

Yes, the code is completely omitted if the symbol isn't defined.是的,如果未定义符号,则完全省略代码。 It can be absolute garbage code - the compiler ignores it completely;它可以是绝对垃圾代码——编译器完全忽略它; it just looks for the end (either #endif or #else ).它只是寻找结尾( #endif#else )。

The question about whether that suits your needs for demo/paid versions is somewhat different, but the answer from the technical perspective of "what gets included in the output" is simple.关于这是否适合您对演示/付费版本的需求的问题有些不同,但从“输出中包含的内容”的技术角度来看,答案很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM