gpt4 book ai didi

c# - 尝试编译调用 native 代码的类时,Mono 编译器崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 20:23:42 25 4
gpt4 key购买 nike

我有以下类,它使用 Mono(在 mac 中)将一些运行循环调用从 native 代码映射到 C#,但编译器无法正常工作,在尝试编译它时它总是崩溃。我已将它移到一个单独的项目中,该项目仅包含 this 类,但它仍然存在问题,因此问题似乎出在这段代码上。

有人见过这样的东西吗?

using System;
using System.Threading;

namespace Integration.Mac
{

public class CommonRunLoop
{

static IntPtr runLoopReference;

public static IntPtr CommonRunLoopReference {
get {
return runLoopReference;
}
}

static CommonRunLoop() {

AutoResetEvent lockObj = new AutoResetEvent (true);

Thread runLoop = new Thread (delegate() {
runLoopReference = CFRunLoopGetCurrent ();
lockObj.Set ();
CFRunLoopRun ();
});

runLoop.IsBackground = true;
runLoop.Name = "Common-Sync-Run-Loop";
lockObj.WaitOne();
}

[DllImport ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")]
extern static IntPtr CFRunLoopGetCurrent ();

[DllImport ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")]
extern static IntPtr CFRunLoopGetMain ();

[DllImport ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")]
extern static void CFRunLoopRun ();

}
}

编译器输出如下:

Building Solution: calling-native-code-mono (Debug)

Building: calling-native-code-mono (Debug) Performing main compilation... /Library/Frameworks/Mono.framework/Versions/2.10.9/bin/dmcs /noconfig "/out:/Users/mauricio/Projects/calling-native-code-mono/bin/Debug/calling-native-code-mono.dll" "/r:/Library/Frameworks/Mono.framework/Versions/2.10.9/lib/mono/4.0/System.dll" "/r:/Library/Frameworks/Mono.framework/Versions/2.10.9/lib/mono/4.0/System.Core.dll" /nologo /warn:4 /debug:full /optimize- /codepage:utf8 "/define:DEBUG" /t:library "/Users/mauricio/Projects/calling-native-code-mono/CommonRunLoop.cs" "/Users/mauricio/Projects/calling-native-code-mono/AssemblyInfo.cs"

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Mono.CSharp.EmitContext.Emit (OpCode opcode, Mono.CSharp.MethodSpec method) [0x00000] in :0 at Mono.CSharp.New.Emit (Mono.CSharp.EmitContext ec, IMemoryLocation target) [0x00000] in :0 at Mono.CSharp.VariableReference.EmitAssign (Mono.CSharp.EmitContext ec, Mono.CSharp.Expression source, Boolean leave_copy, Boolean prepare_for_load) [0x00000] in :0 at Mono.CSharp.Assign.Emit (Mono.CSharp.EmitContext ec, Boolean is_statement) [0x00000] in :0 at Mono.CSharp.Assign.EmitStatement (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.BlockVariableDeclaration.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Constructor.Emit () [0x00000] in :0
at Mono.CSharp.TypeContainer.EmitType () [0x00000] in :0 at Mono.CSharp.ModuleContainer.Emit () [0x00000] in :0 at Mono.CSharp.AssemblyDefinition.Emit () [0x00000] in :0 at Mono.CSharp.Driver.Compile () [0x00000] in :0 at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Mono.CSharp.EmitContext.Emit (OpCode opcode, Mono.CSharp.MethodSpec method) [0x00000] in :0 at Mono.CSharp.New.Emit (Mono.CSharp.EmitContext ec, IMemoryLocation target) [0x00000] in :0 at Mono.CSharp.VariableReference.EmitAssign (Mono.CSharp.EmitContext ec, Mono.CSharp.Expression source, Boolean leave_copy, Boolean prepare_for_load) [0x00000] in :0 at Mono.CSharp.Assign.Emit (Mono.CSharp.EmitContext ec, Boolean is_statement) [0x00000] in :0 at Mono.CSharp.Assign.EmitStatement (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.BlockVariableDeclaration.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Constructor.Emit () [0x00000] in :0
at Mono.CSharp.TypeContainer.EmitType () [0x00000] in :0 at Mono.CSharp.ModuleContainer.Emit () [0x00000] in :0 at Mono.CSharp.AssemblyDefinition.Emit () [0x00000] in :0 at Mono.CSharp.Driver.Compile () [0x00000] in :0 at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in :0

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Mono.CSharp.EmitContext.Emit (OpCode opcode, Mono.CSharp.MethodSpec method) [0x00000] in :0 at Mono.CSharp.New.Emit (Mono.CSharp.EmitContext ec, IMemoryLocation target) [0x00000] in :0 at Mono.CSharp.VariableReference.EmitAssign (Mono.CSharp.EmitContext ec, Mono.CSharp.Expression source, Boolean leave_copy, Boolean prepare_for_load) [0x00000] in :0 at Mono.CSharp.Assign.Emit (Mono.CSharp.EmitContext ec, Boolean is_statement) [0x00000] in :0 at Mono.CSharp.Assign.EmitStatement (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.BlockVariableDeclaration.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Constructor.Emit () [0x00000] in :0
at Mono.CSharp.TypeContainer.EmitType () [0x00000] in :0 at Mono.CSharp.ModuleContainer.Emit () [0x00000] in :0 at Mono.CSharp.AssemblyDefinition.Emit () [0x00000] in :0 at Mono.CSharp.Driver.Compile () [0x00000] in :0 at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Mono.CSharp.EmitContext.Emit (OpCode opcode, Mono.CSharp.MethodSpec method) [0x00000] in :0 at Mono.CSharp.New.Emit (Mono.CSharp.EmitContext ec, IMemoryLocation target) [0x00000] in :0 at Mono.CSharp.VariableReference.EmitAssign (Mono.CSharp.EmitContext ec, Mono.CSharp.Expression source, Boolean leave_copy, Boolean prepare_for_load) [0x00000] in :0 at Mono.CSharp.Assign.Emit (Mono.CSharp.EmitContext ec, Boolean is_statement) [0x00000] in :0 at Mono.CSharp.Assign.EmitStatement (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.BlockVariableDeclaration.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Block.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in :0 at Mono.CSharp.Constructor.Emit () [0x00000] in :0
at Mono.CSharp.TypeContainer.EmitType () [0x00000] in :0 at Mono.CSharp.ModuleContainer.Emit () [0x00000] in :0 at Mono.CSharp.AssemblyDefinition.Emit () [0x00000] in :0 at Mono.CSharp.Driver.Compile () [0x00000] in :0 at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in :0 Build complete -- 1 error, 0 warnings

---------------------- Done ----------------------

Build: 1 error, 0 warnings

最佳答案

添加:

using System.Runtime.InteropServices;

在文件的开头修复它,DLLImport 位于该 namespace 中。不知道为什么编译器不只是提示它而不是崩溃。

已填写错误报告 here .

关于c# - 尝试编译调用 native 代码的类时,Mono 编译器崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11955109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com