C# 不安全代码中的编译错误

发布于 2024-10-07 08:56:18 字数 384 浏览 0 评论 0原文

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

  1. 运算符“*”无法应用于“System.IntPtr”类型的操作数和 'int'

  2. 常量值 '325486741' 无法转换为 'int'(使用 'unchecked' 语法)覆盖)

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

  4. 无法将类型 'long' 隐式转换为 'byte*'。存在显式转换(是否缺少强制转换?)

  5. 无效的表达式术语“ref”

所有这些都发生在“不安全”方法中。

这些如何解决呢?

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. Operator '*' cannot be applied to operands of type 'System.IntPtr' and 'int'

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

  3. Cannot convert type 'string' to 'char*'

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

  5. Invalid expression term 'ref'

All these are occurring in 'unsafe' methods.

How to resolve these ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

八巷 2024-10-14 08:56:18

我们需要查看您的代码,但我想说“不安全”部分与错误无关,因为这些似乎是转换等问题。

以下是一些可能有帮助的信息:

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

首先尝试转换为 int 或 long。

  1. 常量值“325486741”无法转换为“int”(使用“unchecked”语法覆盖)

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

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

尝试使用:

 fixed (char* pChar = my_string) { ... }
  1. 无法将类型“long”隐式转换为“byte*”。存在显式转换(是否缺少强制转换?)

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

  1. 无效的表达式术语“ref”

如果没有代码,我无法对此进行太多说明。

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. Operator '*' cannot be applied to operands of type 'System.IntPtr' and 'int'

Try casting to an int or long first.

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

Try using unchecked((int)variable).

  1. Cannot convert type 'string' to 'char*'

Try using:

 fixed (char* pChar = my_string) { ... }
  1. Cannot implicitly convert type 'long' to 'byte*'. An explicit conversion exists (are you missing a cast?)

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

  1. Invalid expression term 'ref'

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文