C#编译不安全代码中的错误 [英] C# compile errors in unsafe code

查看:79
本文介绍了C#编译不安全代码中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译VS 2010 c#项目(.NET 4.0,任何CPU,允许不安全代码=已选中),我们会遇到各种编译错误,如下所示:

Compiling a VS 2010 c# project (.NET 4.0, any CPU, allow unsafe code = checked) we are getting a variety of compile errors as below:

  1. 运算符'*'不能应用于类型为'System.IntPtr'和'int'的操作数

  1. Operator '*' cannot be applied to operands of type 'System.IntPtr' and 'int'

常量值"325486741"不能转换为"int"(使用"unchecked"语法覆盖)

Constant value '325486741' cannot be converted to a 'int' (use 'unchecked' syntax to override)

无法将类型'string'转换为'char *'

Cannot convert type 'string' to 'char*'

无法将类型'long'隐式转换为'byte *'.存在显式转换(您是否缺少演员表?)

Cannot implicitly convert type 'long' to 'byte*'. An explicit conversion exists (are you missing a cast?)

无效的表达方式'ref'

Invalid expression term 'ref'

所有这些都是以不安全"的方式发生的.

All these are occurring in 'unsafe' methods.

如何解决这些问题?

推荐答案

我们需要查看您的代码,但是我想说不安全"部分与错误无关,因为这些似乎与铸造等.

We'd need to see your code, but I'd say that the "unsafe" part is irrelevant to the errors, since those seem to be problems with casting and such.

以下一些信息可能会有所帮助:

Here's some info that might help:

  1. 运算符'*'不能应用于类型为'System.IntPtr'和'int'的操作数

尝试先强制转换为int或long.

Try casting to an int or long first.

  1. 常量"325486741"不能转换为"int"(使用"unchecked"语法覆盖)

尝试使用unchecked((int)variable).

Try using unchecked((int)variable).

  1. 无法将类型'string'转换为'char *'

尝试使用:

 fixed (char* pChar = my_string) { ... }

  1. 无法将类型'long'隐式转换为'byte *'.存在显式转换(您是否缺少演员表?)

尝试强制转换:byte * pB =(byte *)value;

Try casting: byte* pB = (byte*)value;

  1. 无效的表达方式"ref"

如果没有代码,我不能说太多.

I can't say much about this one without the code.

这篇关于C#编译不安全代码中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆