为什么我会收到错误“仅在使用 /unsafe 进行编译时才可能出现不安全代码”?

问题描述 投票:0回答:8

为什么会出现以下错误?

只有使用“/unsafe”编译时才可能出现不安全代码?

我使用 C# 和 Visual Studio 2008 在 Windows CE 上进行编程。

c# visual-studio-2008 windows-ce unsafe
8个回答
300
投票

要使用不安全的代码块,必须在打开 /unsafe 开关的情况下编译项目。

打开项目的属性,转到

Build
选项卡并选中
Allow unsafe code
复选框。


135
投票

这是截图:

Unsafe screenshot

ََََََََ


4
投票

可能是因为您使用了不安全的代码。

您是否在某处使用指针或非托管程序集执行某些操作?


4
投票

在代码中搜索

unsafe
块或语句。这些仅在使用
/unsafe
编译时才有效。


4
投票

要使用不安全代码块,请打开项目的属性,转到 Build 选项卡并选中 Allow unsafe code 复选框,然后编译并运行。

class myclass
{
     public static void Main(string[] args)
     {
         unsafe
         {
             int iData = 10;
             int* pData = &iData;
             Console.WriteLine("Data is " + iData);
             Console.WriteLine("Address is " + (int)pData);
         }
     }
}

输出:

Data is 10
Address is 1831848

3
投票

对于使用 Rider 的每个人,您必须选择您的项目>右键单击>属性>配置,然后选择调试和发布并选中“允许不安全代码”


3
投票

也可以直接在.csproj文件中将

AllowUnsafeBlocks
标签添加到
PropertyGroup

<PropertyGroup>
    <TargetFrameworks>netcoreapp3.1; net472</TargetFrameworks>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

0
投票

我将此行添加到项目文件中

<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
它对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.