简体   繁体   English

如何在“visual studio code”中运行不安全的代码?

[英]How to run unsafe code in “visual studio code”?

I am using Visual studio code and when I try to run an unsafe code it throws the following error ""message": Unsafe code may only appear if compiling with /unsafe"我正在使用 Visual Studio 代码,当我尝试运行不安全代码时,它会引发以下错误“消息”:仅当使用 /unsafe 编译时才会出现不安全代码

and as in visual studio, it does not have option like project->properties.和 Visual Studio 一样,它没有像 project->properties 这样的选项。

unsafe (C# Compiler Options) 不安全(C# 编译器选项)

  1. To set this compiler option in the Visual Studio development environment Open the project's Properties page.在 Visual Studio 开发环境中设置此编译器选项 打开项目的属性页。

    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:在 csproj 文件中添加此选项 打开项目的 .csproj 文件,并添加以下元素:

XML XML

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

Usage用法

Method level方法级别

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

Inline Block内联块

...

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

Class Level班级

public unsafe class Blah {}

In the .csproj file, just add.csproj文件中,只需添加

<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

to any <PropertyGroup> block.到任何<PropertyGroup>块。

No need to add anything to task.json .无需task.json添加任何task.json

All not working in my netcoreapp3.1 C# Project所有在我的 netcoreapp3.1 C# 项目中都不起作用

This helped (in .vscode/tasks.json):这有帮助(在 .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"
        },

works for the 'dotnet build' command, not for the green launch button适用于“dotnet build”命令,不适用于绿色启动按钮

also working if executed from the terminal: 'dotnet build -p:unsafe=true'如果从终端执行也可以工作:'dotnet build -p:unsafe=true'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Visual Studio代码中运行.Net代码? - How to run .Net code in visual studio code? 不安全的代码有多不安全? - How unsafe is unsafe code? 如何运行可能不安全的用户代码? - How to run user code that might be unsafe? 不安全的代码将无法在Visual Studio 2015上编译 - Unsafe code won't compile on Visual Studio 2015 无法在Visual Studio 2013中发布带有不安全代码的WCF服务 - Unable to Publish WCF Service with Unsafe code in Visual Studio 2013 如何在 Visual Studio 2019 .net5.0 上允许发布和调试不安全代码 - How to allow unsafe code for Release and Debug on visual studio 2019 .net5.0 在 Visual Studio App Center for Xamarin iOS 中构建失败:只有在使用 /unsafe 编译时才会出现不安全的代码 - Build fails in Visual Studio App Center for Xamarin iOS: Unsafe code may only appear if compiling with /unsafe 如何运行此 C# 代码。 使用 Visual Studio - How to run this C# code. using Visual Studio OSX 上的 Visual Studio Code 如何导入 sln/csproj 并运行? - How can Visual Studio Code on OSX import sln/csproj and run? 如何在Visual Studio 2015和Windows 10上运行DirectX代码示例? - How to run DirectX code samples on Visual Studio 2015 and Windows 10?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM