You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET5项目如何根据目标平台实现部分代码的条件编译

.NET 5 项目平台条件编译配置失效问题
  • 新建基于.NET 5的项目。
  • 尝试了C#中基于平台实现导入的预处理指令相关解决方案,所有修改项目文件的尝试最终都导致项目无法加载,只能回滚配置才能让项目正常可用。

以下是想要编译运行的代码,请注意Windows Event仅可在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

尝试了以下两种解决方案均未成功,只要修改项目文件,项目就无法加载:

<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 EST 补充
Eriawan给出的答案看起来很有前景,但实际测试无效,哪怕将平台设置为“x64”,所有相关代码仍然是灰色未激活状态。不清楚具体原因,怀疑和使用的.NET 5版本有关。另外,输入“#if”时,IntelliSense会展示一些可用变量,但其中没有“WINDOWS”。不确定是不是所有可用常量都应该展示,可能存在部分缺失的情况,但可以确定“WINDOWS”常量无法生效。

内容的提问来源于stack exchange,提问作者Eric Ouellet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 18:51:02