本文介绍了如何在“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)

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"中运行不安全的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 06:41
查看更多