我正在使用 Visual Studio 代码,当我尝试运行不安全代码时,它会抛出以下错误““消息”:不安全代码可能仅在使用/unsafe 编译时出现”
和在 visual studio 中一样,它没有像 project->properties 这样的选项。
unsafe (C# Compiler Options)
To set this compiler option in the Visual Studio development environment Open the project's Properties page.
Click the Build property page.
Select the Allow Unsafe Code check box.
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>
用法
方法级别
unsafe static void FastCopy(byte[] src, byte[] dst, int count)
{
// Unsafe context: can use pointers here.
}
内联 block
...
unsafe
{
// Unsafe context: can use pointers here.
}
类(class)
public unsafe class Blah {}
我是一名优秀的程序员,十分优秀!