在运行时从iOS上的本机方法创建委托

Fel*_* K. 3 c# xamarin.ios

这是MonoTouch特有的问题.

我目前正在为OpenGL开发一个包装器,它与OpenTK这样的包装器完全不同.此包装器用于使用OpenGL实现更快的开发.

方法不声明如下:void glGenTextures(Int32 n, UInt32[] textures);,他们宣称喜欢void glGenTextures(Int32 count, [Out]TextureHandle[] textures)在那里TextureHandle与同样大小的一个结构UInt32.

在Windows上,我可以使用GetProcAddress,wglGetProcAddressMarshal.GetDelegateForFunctionPointer从方法指针创建委托,但如何在iOS上使用MonoTouch执行此操作.有什么方法可以解决这个问题,还是monotouch不支持?

Rol*_*nge 6

从MonoTouch 5.4开始,这是可能的.您需要创建一个与托管方法具有相同签名的委托,并使用以下MonoNativeFunctionWrapper属性进行装饰:

[MonoNativeFunctionWrapper]
public delegate void glGenTexturesDelegate (int n, uint[] textures);
Run Code Online (Sandbox Code Playgroud)

现在你可以调用方法:

var del = (glGenTexturesDelegate) Marshal.GetDelegateForFunctionPointer (pointer);
del (n, textures);
Run Code Online (Sandbox Code Playgroud)

也就是说,我相信你做的比你需要的要复杂得多.只需使用P/Invoke:

[llImport("libGLESv2.dll")]
extern static void glGenTextures (int n, uint[] textures);
Run Code Online (Sandbox Code Playgroud)

然后像这样调用它:

glGenTextures (n, textures);
Run Code Online (Sandbox Code Playgroud)