How to run unsafe code in “visual studio code”?

2019-08-05 03:41发布

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.

2条回答
看我几分像从前
2楼-- · 2019-08-05 04:06

In the .csproj file, just add

<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

to any <PropertyGroup> block.

No need to add anything to task.json.

查看更多
不美不萌又怎样
3楼-- · 2019-08-05 04:25

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 {}
查看更多
登录 后发表回答