预处理器指令是否保护服务器代码不受客户端影响?

Do pre-processor directives protect server code from the client?

我正在开发客户端-服务器库。某些 classes 可由客户端或服务器使用,但执行方式不同,并且各自产生的结果略有不同。同样,服务器代码可能包含不会从客户端构建中调用的其他方法。

A​​ class 可能如下所示:

public class StuffDoer {

    public void DoStuff(object msg)
    {
        ServerDoStuff(msg);
        ClientDoStuff(msg);
    }

    [Conditional("SERVER")]
    private void ServerDoStuff(object msg) 
    {
        // Do secret server stuff...
    }

    [Conditional("CLIENT")]
    private void ClientDoStuff(object msg)
    {
        // Do client sutff...
    }

    [Conditional("SERVER")]
    public void DoCoolStuff(object msg)
    {
        // server does cool stuff...
    }

}

我读到 Conditional 属性仍然编译代码,因此会在构建中,这与预处理器指令不同,预处理器指令会完全删除代码甚至不编译它。

我担心不诚实的客户可能会通过解密源代码并弄清楚服务器的工作原理来破解产品。

我的担心是没有根据的,还是我需要放置预处理器指令以隐藏源代码?

根据 documentation:

Applying ConditionalAttribute to a method indicates to compilers that a call to the method should not be compiled into Microsoft intermediate language (MSIL) unless the conditional compilation symbol that is associated with ConditionalAttribute is defined.

当您使用定义的 CLIENT 进行编译时,调用带有 SERVER 标记的方法的代码将不会出现在最终的程序集中。但是该方法的代码将存在。你无法通过这种方式实现你所需要的。

当 "SERVER" 未定义时,标记为 "SERVER" 的方法将始终编译为最终程序集,但将删除对该方法的所有调用。

This is code

This is decompiled result