150

为什么我会收到以下错误?

不安全代码可能仅在使用 /unsafe 编译时出现?

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

4

6 回答 6

287

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

打开项目的属性,转到Build选项卡并选中Allow unsafe code复选框。

于 2010-01-08T09:07:47.880 回答
133

这是一个屏幕截图:

不安全的截图

ََََََََ

于 2011-07-21T05:59:52.983 回答
4

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

您是否在某处使用指针或非托管程序集做某事?

于 2010-01-08T08:52:16.350 回答
4

在代码中搜索unsafe块或语句。这些仅在编译时有效/unsafe

于 2010-01-08T08:52:22.353 回答
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
于 2010-11-05T09:31:28.613 回答
3

对于使用 Rider 的每个人,您必须选择您的项目>右键单击>属性>配置,然后选择 Debug and Release 并检查两者的“允许不安全代码” 。截屏

于 2018-04-22T10:17:45.487 回答