如何在“visual studio code"中运行不安全的代码? [英] How to run unsafe code in "visual studio code"?

查看:172
本文介绍了如何在“visual studio code"中运行不安全的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Visual Studio 代码,当我尝试运行不安全代码时,它会引发以下错误消息":仅当使用/unsafe 编译时才会出现不安全代码

和visual studio一样,它没有像project->properties这样的选项.

解决方案

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

<块引用>

  1. 要在 Visual Studio 开发环境中设置此编译器选项,请打开项目的属性"页面.

    1. 单击构建"属性页.

    2. 选中允许不安全代码复选框.

  2. 在 csproj 文件中添加此选项 打开项目的 .csproj 文件,并添加以下元素:

XML

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

用法

方法级别

unsafe static void FastCopy(byte[] src, byte[] dst, int count){//不安全的上下文:可以在这里使用指针.}

内联块

<代码>...不安全{//不安全的上下文:可以在这里使用指针.}

班级

public unsafe class Blah {}

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"

and as in visual studio, it does not have option like project->properties.

解决方案

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>

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 {}

这篇关于如何在“visual studio code"中运行不安全的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆