MonoDroid的JNI的Java反射来调用一个私有方法 [英] Monodroid JNI for Java reflection to call a private method

查看:721
本文介绍了MonoDroid的JNI的Java反射来调用一个私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个MonoDroid的项目,我需要能够调用私有方法的一类。从在一个相关的问题一个答案,似乎这是可能在Java中通过反射:

In a Monodroid project, I need to be able to call a private method on a class. From an answer on a related question, it seems that this is possible in Java via reflection:

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 文档是空白的。

I need to use this code from Monodroid. Unfortunately, java.lang.reflect is not available in Monodroid. However, it has been suggested that I can run this code using JNI from my Monodroid project. The Xamarin documentation states that inline JNI is possible, without having to bind a whole JAR. Unfortunately, further documentation doesn't say anything more on the subject. Furthermore, the documentation on JNIEnv is blank.

它看起来像我需要 JNIEnv.CallVoidMethod(),但我不知道该怎么做。我找不到一个例子,或进一步文件。

It looks like I need JNIEnv.CallVoidMethod(), but I have no idea how to do it. I can't find an example, or further documentation.

如何使用的java.lang.reflect 在我的MonoDroid的项目,或以其他方式呼吁 ParcelFileDescriptor ? .createPipe >

How can I use java.lang.reflect in my Monodroid project, or in some other way call the private method .createPipe on ParcelFileDescriptor?

推荐答案

应该可以用JNI:的 http://docs.xamarin.com/guides/android/advanced_topics/java_integration_overview/working_with_jni#_Static_Methods

It should be possible with JNI: http://docs.xamarin.com/guides/android/advanced_topics/java_integration_overview/working_with_jni#_Static_Methods

一个粗略的草图未经测试:

A rough untested sketch:

var methodId = JNIEnv.GetStaticMethodID(ParcelFileDescriptor.Class.Handle, 
                                        "createPipe", 
                                        "()[Landroid/os/ParcelFileDescriptor;");
var result = JNIEnv.CallStaticObjectMethod(myCSharpFileDescriptorInstance.Handle,
                                           methodId);

这篇关于MonoDroid的JNI的Java反射来调用一个私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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