C# 不安全代码中的编译错误
编译 VS 2010 c# 项目(.NET 4.0,任何 CPU,允许不安全代码=选中),我们收到各种编译错误,如下所示:
运算符“*”无法应用于“System.IntPtr”类型的操作数和 'int'
常量值 '325486741' 无法转换为 'int'(使用 'unchecked' 语法)覆盖)
无法将类型 'string' 转换为 'char*'
无法将类型 'long' 隐式转换为 'byte*'。存在显式转换(是否缺少强制转换?)
无效的表达式术语“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:
Operator '*' cannot be applied to operands of type 'System.IntPtr' and 'int'
Constant value '325486741' cannot be converted to a 'int' (use 'unchecked' syntax to override)
Cannot convert type 'string' to 'char*'
Cannot implicitly convert type 'long' to 'byte*'. An explicit conversion exists (are you missing a cast?)
Invalid expression term 'ref'
All these are occurring in 'unsafe' methods.
How to resolve these ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们需要查看您的代码,但我想说“不安全”部分与错误无关,因为这些似乎是转换等问题。
以下是一些可能有帮助的信息:
首先尝试转换为 int 或 long。
尝试使用 unchecked((int)variable)。
尝试使用:
尝试强制转换: byte* pB = (byte*)value;
如果没有代码,我无法对此进行太多说明。
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:
Try casting to an int or long first.
Try using unchecked((int)variable).
Try using:
Try casting: byte* pB = (byte*)value;
I can't say much about this one without the code.