C# 为什么会出现错误“不安全代码可能仅在使用 /unsafe 编译时出现”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2026410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:48:56  来源:igfitidea点击:

Why do I get the error "Unsafe code may only appear if compiling with /unsafe"?

c#visual-studio-2008windows-ceunsafe

提问by Gold

Why do I get the following error?

为什么会出现以下错误?

Unsafe code may only appear if compiling with /unsafe"?

只有在使用 /unsafe 编译时才会出现不安全的代码?

I work in C# and Visual Studio 2008 for programming on Windows CE.

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

采纳答案by Guffa

To use unsafe code blocks, the project has to be compiled with the /unsafe switch on.

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

Open the properties for the project, go to the Buildtab and check the Allow unsafe codecheckbox.

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

回答by Gerrie Schenck

Probably because you're using unsafe code.

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

Are you doing something with pointers or unmanaged assemblies somewhere?

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

回答by Richard

Search your code for unsafeblocks or statements. These are only valid is compiled with /unsafe.

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

回答by dipankar ranjan baisya

To use unsafe code blocks, open the properties for the project, go to the Buildtab and check the Allow unsafe codecheckbox, then compile and run.

要使用不安全代码块,请打开项目的属性,转到“构建”选项卡并选中“允许不安全代码”复选框,然后编译并运行。

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);
         }
     }
}

Output:

输出:

Data is 10
Address is 1831848

回答by Manoj Attal

Here is a screenshot:

这是一个屏幕截图:

Unsafe screenshot

不安全截图

????????

?????????

回答by Tobias Brohl

For everybody who uses Rider you have to select your project>Right Click>Properties>Configurations Then select Debug and Release and check "Allow unsafe code" for both.Screenshot

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