如何根据目标平台编译代码(选择部分代码)?

How to compile code (choose part of the code) according to the target platform?

使用 .net 5 的新项目。

我尝试了以下解决方案:Preprocessor directive in C# for importing based on platform

我修改项目文件的所有尝试都会导致项目无法加载。我必须恢复才能拥有一个有效的项目。

这是我想要的代码compile/run。请注意,Windows 事件只能在 Windows 平台上访问,我想将它主要用于初始化,无论出于何种原因,文件系统都无法直接从应用程序访问。

#if WINANY
        /// <summary>
        /// Will log a Windows Event. Event are visible in the Application section with EventViewer.
        /// </summary>
        public static void LogWindowsEvent(LogType logType, string message, Exception ex = null)
        {
            string appName = AppInfo.AppName;
            if (!EventLog.SourceExists(appName))
            {
                EventLog.CreateEventSource(appName, "Application");

                if (logType == LogType.LogException)
                {
                    EventLog.WriteEntry(appName, $"Exception happen: {ex} at StackTrace: {ex.StackTrace}.", EventLogEntryType.Error);
                }
                else
                {
                    EventLogEntryType eventLogEntryType = EventLogEntryType.Error;
                    switch (logType)
                    {
                        case LogType.LogWarning:
                            eventLogEntryType = EventLogEntryType.Warning;
                            break;
                        case LogType.LogMessage:
                            eventLogEntryType = EventLogEntryType.Information;
                            break;
                    }

                    EventLog.WriteEntry(appName, message, eventLogEntryType);
                }
            }
        }

#else
        /// <summary>
        /// 2021-08-30, EO: Empty for the moment (Until I find a solution).
        /// </summary>
        public static void LogWindowsEvent(LogType logType, string message, Exception ex = null)
        {
        }
#endif

我尝试包含这 2 个解决方案,但没有成功。一旦我更改项目文件,项目将不会加载。

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
      <DefineConstants>WIN64;WINDOWS;$(DefineConstants)</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
      <DefineConstants>WIN64;WINDOWS;$(DefineConstants)</DefineConstants>
  </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
          <DefineConstants>WIN32;WINDOWS;$(DefineConstants)</DefineConstants>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
          <DefineConstants>WIN32;WINDOWS;$(DefineConstants)</DefineConstants>
      </PropertyGroup>

第二次尝试:

  <PropertyGroup Condition="$(Platform) == 'x64'">
      <DefineConstants>WIN64;WINANY;$(DefineConstants)</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(Platform) == 'x86'">
      <DefineConstants>WIN32;WINANY;$(DefineConstants)</DefineConstants>
  </PropertyGroup>
  

编辑 2021-08-31,美国东部时间 16 点 49 分

Eriawan 的答案似乎很有希望,但它对我不起作用,当我为“x64”设置时,所有代码都保持灰色。我不知道为什么。也许是因为我使用 .net 5?此外,当我键入“#if”时,智能感知会向我显示一些变量,但“WINDOWS”不存在。我不知道是否所有的可能性都应该存在,也许有些可能会丢失。但是“WINDOWS”绝对行不通。

没有 WINANY 常数来确定 OS 您的代码运行的平台。自 .NET 3.1 起,可用的常用常量为 WINDOWS、ANDROID、LINUX、IOS.

您上面的代码可以简化为:

        /// <summary>
        /// Will log a Windows Event. Event are visible in the Application section with EventViewer.
        /// </summary>
        public static void LogWindowsEvent(LogType logType, string message, Exception ex = null)
        {
#if WINDOWS
            string appName = AppInfo.AppName;
            if (!EventLog.SourceExists(appName))
            {
                EventLog.CreateEventSource(appName, "Application");

                if (logType == LogType.LogException)
                {
                    EventLog.WriteEntry(appName, $"Exception happen: {ex} at StackTrace: {ex.StackTrace}.", EventLogEntryType.Error);
                }
                else
                {
                    EventLogEntryType eventLogEntryType = EventLogEntryType.Error;
                    switch (logType)
                    {
                        case LogType.LogWarning:
                            eventLogEntryType = EventLogEntryType.Warning;
                            break;
                        case LogType.LogMessage:
                            eventLogEntryType = EventLogEntryType.Information;
                            break;
                    }

                    EventLog.WriteEntry(appName, message, eventLogEntryType);
                }
            }
#else
          // Empty code for non Windows
#endif
        }

有关更多信息,另请参阅有关 .NET Core 设计文档的官方文档:

https://github.com/dotnet/designs/blob/main/accepted/2020/net5/net5.md#scenarios-and-user-experience