0

I'm developing a client-server library. Some of the classes can be used by either the client or the server but are executed differently and yield slightly different results for each. As well, the server code may contain additional methods that will not be called from the client build.

A class may look like this:

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...
    }

}

I've read that the Conditional attribute still compiles the code and would therefore be in the build, unlike pre-processor directives which would completely remove the code and not even compile it.

I'm concerned that a dishonest client may hack the product by unobfuscating the source code and figure out how the server works.

Are my fears unfounded or would I need to place pre-processor directives in order to hide the source code?

  • 1
    If I had to choose between the two, my choice would be pre-processor directives. `ConditionalAttribute` will not hide the "innards" of your class because the server specific code will still be compiled to MSIL (only the calls to the methods will be removed). Also, `ConditionalAttribute` is pretty limited as to where and how you can apply it: only void returning methods and classes deriving from `Attribute` – InBetween Feb 10 '17 at 22:06

2 Answers2

1

According to the 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.

When you compile with CLIENT defined, the code to call a method marked with SERVER will not be present in the final assembly. But the code for the method will be present. You cannot achieve what you need in this way.

Damian
  • 2,752
  • 1
  • 29
  • 28
  • I'm not sure that is what it says. It says that any *call* to a method marked with the conditional attribute will not be compiled, but the method itself will be, which is what the OP is trying to avoid. – InBetween Feb 10 '17 at 21:57
  • @InBetween you are correct, I have edited the answer – Damian Feb 10 '17 at 22:09
0

When "SERVER" is not defined, The method be marked as "SERVER" will always compile to the final assembly but all of callings to the method will be removed.

This is code

This is decompiled result

Lake Chan
  • 93
  • 1
  • 5