Monodroid JNI for Javareflection调用私有方法
在Monodroid项目中,我需要能够在类上调用私有方法。 从相关问题的答案来看,似乎这可以通过反思在Java中实现:
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import android.os.ParcelFileDescriptor; ... ParcelFileDescriptor pipe[] = null; try { Method createPipeMethod = ParcelFileDescriptor.class.getDeclaredMethod("createPipe"); pipe = (ParcelFileDescriptor[]) createPipeMethod.invoke(null); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); }
我需要使用Monodroid的代码。 不幸的是, java.lang.reflect
在Monodroid中不可用 。 但是,有人建议我可以使用Monodroid项目中的JNI运行此代码。 Xamarin文档声明内联JNI是可能的 ,而不必绑定整个JAR。 不幸的是, 进一步的文档没有更多关于这个主题的内容。 此外, JNIEnv的文档是空白的。
看起来我需要JNIEnv.CallVoidMethod()
,但我不知道该怎么做。 我找不到示例或进一步的文档。
如何在我的Monodroid项目中使用java.lang.reflect
,或以其他方式在.createPipe
上调用私有方法ParcelFileDescriptor
?
您是否尝试在Android.OS.ParcelFileDescriptor
上使用C#reflection?
http://docs.mono-android.net/index.aspx?link=T%3AAndroid.OS.ParcelFileDescriptor
我还没有尝试过,但如果Mono for Android甚至包装了Java类的私有成员,那么仅仅使用C#reflection就足够了。
如果失败,您可以继续尝试JNI。
应该可以使用JNI: http : //docs.xamarin.com/guides/android/advanced_topics/java_integration_overview/working_with_jni#_Static_Methods
粗略的未经测试的草图:
var methodId = JNIEnv.GetStaticMethodID(ParcelFileDescriptor.Class.Handle, "createPipe", "()[Landroid/os/ParcelFileDescriptor;"); var result = JNIEnv.CallStaticObjectMethod(myCSharpFileDescriptorInstance.Handle, methodId);