如何更改#define

How can I change #define

本文关键字:#define 何更改      更新时间:2023-10-16

如何更改#define TRIAL为#define PREMIUM,当用户输入一些注册键时。

谢谢。

//#define TRIAL
#define PREMIUM
using System;
using System.Diagnostics;
using System.Reflection;
namespace Attributes
{
    [Obsolete("This is an old class. Use new class instead!")] 
    class Test
    {
        [Conditional("TRIAL")]
        void Trial()
        {
            Console.WriteLine("Trial");
        }
        [Conditional("PREMIUM")]
        void Release()
        {
            Console.WriteLine("PREMIUM");
        }
......................................

听起来您可能误解了预处理器指令。您应该将这类信息以某种方式加密存储在用户的机器上,并将其加载到变量中,或者更好的方法是发布两个带有升级选项的版本。这将更加稳定和安全。

如果您选择采用这种方法,那么您可以使用预处理器指令为每个程序的发行版本编译不同版本的库和代码。

#define是编译时指令,所以你不能使用它…相反,您应该使用传统的if:

class Test
{
    void DoAction() {
      if(trial) { Console.WriteLine("Trial"); }
      else { Console.WriteLine("PREMIUM");
    }
}