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

如何在Mono环境下构建Visual Studio 2022的.NET Framework控制台项目?

问题:让.NET Framework控制台应用在Mono环境运行(Visual Studio 2022)

我正在使用Visual Studio 2022,目标是让.NET Framework控制台应用程序在Mono环境中运行(无需Unity或其他工具)。

控制台应用程序代码

internal class Program
{
    private static Task Main(string[] args)
    {
        if (Type.GetType("Mono.Runtime") != null)
        {
            Console.WriteLine("Mono!"); // 预期在控制台输出'Mono!'
        }
        else
        {
            Console.WriteLine("Something other!");
        }
        return Task.CompletedTask;
    }
}

原项目.csproj配置

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net472</TargetFramework>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DebugType>none</DebugType>
    <DefineConstants>$(DefineConstants)TRACE</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <DebugType>none</DebugType>
    <DefineConstants>$(DefineConstants)TRACE</DefineConstants>
  </PropertyGroup>

</Project>

已尝试方法

  • MonoHelper扩展:仅支持旧版Visual Studio,已停止维护;
  • 使用Mono msbuild构建:执行命令C:\Program Files\Mono>msbuild "path_to_project_here.csproj" -p:Configuration=Release时出现错误:
    "path_to_project_here.csproj" (default target) (1) ->
      path_to_project_here.csproj : error MSB4237: The SDK resolver type "Dot
    NetMSBuildSdkResolver" failed to load. The type initializer for 'Microsoft.DotNet.DotNetSdkResolver.VSSettings' threw a
    n exception.
    
  • xbuild已被弃用,改用Mono msbuild;
  • 不想使用MonoDevelop,寻求简便解决方法。

解决方案

1. 改用传统.NET Framework项目格式

Mono的MSBuild对Microsoft.NET.Sdk格式的项目支持不佳,将项目改为传统的非SDK格式即可解决编译错误:

替换原.csproj内容为以下代码:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{YOUR_PROJECT_GUID}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>YourAppNamespace</RootNamespace>
    <AssemblyName>YourAppName</AssemblyName>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <DebugType>none</DebugType>
    <DefineConstants>TRACE</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>false</DebugSymbols>
    <DebugType>none</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>none</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="Program.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

注意:替换{YOUR_PROJECT_GUID}为原项目的GUID(可在原.csproj或.sln文件中找到),YourAppNamespace和YourAppName改为实际的命名空间和程序集名称。

2. 在Visual Studio中编译项目

修改后直接在Visual Studio 2022中正常编译,生成Release版本的exe文件。

3. 在Mono环境中运行程序

打开Mono的命令行工具,执行以下命令运行生成的exe:

mono path_to_your_exe\YourAppName.exe

此时控制台会输出Mono!,说明程序已在Mono环境中正常运行。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 09:15:32