如何 运行 "visual studio code" 中的不安全代码?

How to run unsafe code in "visual studio code"?

我正在使用 Visual studio 代码,当我尝试 运行 不安全代码时,它会抛出以下错误“"message":不安全代码可能仅在使用 /unsafe 编译时出现”

和visual studio一样,它没有项目->属性这样的选项。

unsafe (C# Compiler Options)

  1. To set this compiler option in the Visual Studio development environment Open the project's Properties page.

    1. Click the Build property page.

    2. Select the Allow Unsafe Code check box.

  2. To add this option in a csproj file Open the .csproj file for a project, and add the following elements:

XML

  <PropertyGroup>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>

用法

方法级别

unsafe static void FastCopy(byte[] src, byte[] dst, int count)  
{  
    // Unsafe context: can use pointers here.  
}  

内联块

...

unsafe  
{  
    // Unsafe context: can use pointers here.  
}

Class 级别

public unsafe class Blah {}

.csproj文件中,只需添加

<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

任何<PropertyGroup>块。

无需task.json添加任何内容。

在我的 netcoreapp3.1 C# 项目中都不起作用

这有帮助(在 .vscode/tasks.json):

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/rest.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary",
                "/unsafe"
            ],
            "problemMatcher": "$msCompile"
        },

适用于 'dotnet build' 命令,不适用于绿色启动按钮

如果从终端执行也可以工作:'dotnet build -p:unsafe=true'