且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-10-29 17:32:02

不安全(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 {}