[英]Monodroid JNI for Java reflection to call a private method
In a Monodroid project, I need to be able to call a private method on a class. 在Monodroid项目中,我需要能够在类上调用私有方法。 From an answer on a related question , it seems that this is possible in Java via reflection: 从一个相关问题的答案来看,似乎可以通过反射在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);
}
I need to use this code from Monodroid. 我需要使用Monodroid的代码。 Unfortunately, java.lang.reflect
is not available in Monodroid . 不幸的是, Monodroid中没有java.lang.reflect
。 However, it has been suggested that I can run this code using JNI from my Monodroid project. 但是,建议我可以使用Monodroid项目中的JNI运行此代码。 The Xamarin documentation states that inline JNI is possible , without having to bind a whole JAR. Xamarin文档指出,可以内联JNI ,而不必绑定整个JAR。 Unfortunately, further documentation doesn't say anything more on the subject. 不幸的是, 进一步的文档没有进一步说明 。 Furthermore, the documentation on JNIEnv is blank. 此外, JNIEnv的文档为空白。
It looks like I need JNIEnv.CallVoidMethod()
, but I have no idea how to do it. 看来我需要JNIEnv.CallVoidMethod()
,但我不知道如何执行。 I can't find an example, or further documentation. 我找不到示例或更多文档。
How can I use java.lang.reflect
in my Monodroid project, or in some other way call the private method .createPipe
on ParcelFileDescriptor
? 如何在Monodroid项目中使用java.lang.reflect
,或以其他方式在.createPipe
上调用私有方法ParcelFileDescriptor
?
Did you try to use C# reflection on Android.OS.ParcelFileDescriptor
? 您是否尝试在Android.OS.ParcelFileDescriptor
上使用C#反射?
http://docs.mono-android.net/index.aspx?link=T%3AAndroid.OS.ParcelFileDescriptor http://docs.mono-android.net/index.aspx?link=T%3AAndroid.OS.ParcelFileDescriptor
I did not yet try it, but if Mono for Android even wraps private members of a Java class, simply using C# reflection might be enough. 我还没有尝试过,但是如果Android版Mono甚至包装Java类的私有成员,仅使用C#反射就足够了。
If this failed, you can pursue on the JNI attempt. 如果失败,则可以尝试JNI。
It should be possible with JNI: http://docs.xamarin.com/guides/android/advanced_topics/java_integration_overview/working_with_jni#_Static_Methods 使用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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.