[英]Monodroid JNI for Java reflection to call a private method
在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的代码。 不幸的是, Monodroid中没有java.lang.reflect
。 但是,建议我可以使用Monodroid项目中的JNI运行此代码。 Xamarin文档指出,可以内联JNI ,而不必绑定整个JAR。 不幸的是, 进一步的文档没有进一步说明 。 此外, JNIEnv的文档为空白。
看来我需要JNIEnv.CallVoidMethod()
,但我不知道如何执行。 我找不到示例或更多文档。
如何在Monodroid项目中使用java.lang.reflect
,或以其他方式在.createPipe
上调用私有方法ParcelFileDescriptor
?
您是否尝试在Android.OS.ParcelFileDescriptor
上使用C#反射?
http://docs.mono-android.net/index.aspx?link=T%3AAndroid.OS.ParcelFileDescriptor
我还没有尝试过,但是如果Android版Mono甚至包装Java类的私有成员,仅使用C#反射就足够了。
如果失败,则可以尝试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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.