从iOS上的本地方法运行时创建委托 [英] Create delegate at runtime from native method on iOS

查看:152
本文介绍了从iOS上的本地方法运行时创建委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个特定的MonoTouch - 的问题。

我目前正在开发的OpenGL的包装是从像OpenTK包装完全不同。这种包装是用来启用OpenGL的更快发展

I'm currently developing a wrapper for OpenGL which is quite different from wrappers like OpenTK. This wrapper is used to enable faster development with OpenGL.

方法不声明如下:无效glGenTextures(的Int32 N,UInt32的[]纹理) ; ,它们被声明像无效glGenTextures(的Int32算,[出] TextureHandle []纹理),其中 TextureHandle 是相同尺寸为 UInt32的

Methods are not declared like this: void glGenTextures(Int32 n, UInt32[] textures);, they are declared like void glGenTextures(Int32 count, [Out]TextureHandle[] textures) where TextureHandle is a structure with the same size as a UInt32.

在Windows上,我可以使用 GetProcAddress的 wglGetProcAddress Marshal.GetDelegateForFunctionPointer 从方法指针创建委托,但如何与MonoTouch的做iOS上。有什么办法来这还是它不受MonoTouch的被支持

On Windows i can use GetProcAddress, wglGetProcAddress and Marshal.GetDelegateForFunctionPointer to create a delegate from a method pointer, but how to do this on iOS with MonoTouch. Is there any way to this or is it not supported by monotouch yet?

推荐答案

用的MonoTouch 5.4开始这是可能的。您需要创建具有相同签名的管理方法的委托,并与 MonoNativeFunctionWrapper 属性装饰它:

Starting with MonoTouch 5.4 this is possible. You need to create a delegate with the same signature as the managed method, and decorate it with the MonoNativeFunctionWrapper attribute:

[MonoNativeFunctionWrapper]
public delegate void glGenTexturesDelegate (int n, uint[] textures);

现在,你可以调用方法:

Now you can call the method:

var del = (glGenTexturesDelegate) Marshal.GetDelegateForFunctionPointer (pointer);
del (n, textures);

这是说,我相信你正在做这个有很多更复杂的比你需要。只需使用的P / Invoke:

That said, I believe you're doing this a lot more complicated than you need to. Just use a P/Invoke:

[llImport("libGLESv2.dll")]
extern static void glGenTextures (int n, uint[] textures);



然后调用它是这样的:

and then call it like this:

glGenTextures (n, textures);

这篇关于从iOS上的本地方法运行时创建委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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