How to replace Preprocessor directives with C# code,Alternative for preprocessor directives

146 Views Asked by At

We are using preprocessor directives like trail and licensed, so we have two installers for trail and licensed. When the user want to upgrade from trail to licensed they have to uninstall trail and install Licensed and vice versa. Now we want to have only one installer that means we want to get rid of the process of reinstalling the application for trail to licensed and have only one installer which serves Trail and Licensed users.so my question is based on some license file that is DAT file, how we should change preprocessed code with C# code what are the alternatives to change code after user upgrades or degrades License. If there is any change in License then we need to come to Main method and reload session and all exception handlers, engines again with respective mode on. Please suggest how to code for this kind of challenge.

 class Program
    {
        static void Main(string[] args)
        {
#if Trail
        SomeStaticClass.PropertyId = "10023";
        //trail based code
#elif LICENSED
            SomeStaticClass.PropertyId = "10024";
            //licensed code
#endif

            //register some events based on trail and Licensed
            //create session based on trail and Licensed
            //license check is happening after all the sessions and environments are created 

        }
    }

    public static class SomeStaticClass
    {
        public static string PropertyId { get; set; }
    }
1

There are 1 best solutions below

1
On

If downloading your trial version gives people all the compiled code of the licensed version, then some people may try to work out how your licensing test is implemented and break it.

I would instead refactor your application to create trial and licensed implementations of the same interfaces. Created from either a trial factory class or a licensed factory. With all the licensed classes in a separate assembly, which you do not ship with your trial version.

Your main method can then locate the licensed assembly, load it, and discover the factory class via reflection.

If you really want to ship only a single installer, I would ship the licensed assembly encrypted. Then your registration process can transmit and store the encryption key, so your main method can decrypt the assembly into memory and load it. Without the encryption key, the assembly would be completely useless.