C# Monodroid JNI for Java反射调用私有方法
在Monodroid项目中,我需要能够调用类上的私有方法。从中可以看出,这在Java中似乎可以通过反射实现:C# Monodroid JNI for Java反射调用私有方法,c#,java,java-native-interface,xamarin.android,jnienv,C#,Java,Java Native Interface,Xamarin.android,Jnienv,在Monodroid项目中,我需要能够调用类上的私有方法。从中可以看出,这在Java中似乎可以通过反射实现: import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import android.os.ParcelFileDescriptor; ... ParcelFileDescriptor pipe[] = null; try { Method createPipe
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项目中的JNI运行这段代码。这个瓶子,不用绑一整罐。不幸的是,他没有在这个问题上多说什么。此外,报告是空白的
看起来我需要JNIEnv.CallVoidMethod()
,但我不知道怎么做。我找不到示例或进一步的文档
如何在我的Monodroid项目中使用java.lang.reflect
,或者以其他方式调用ParcelFileDescriptor
上的私有方法.createPipe
?您是否尝试在Android.OS.ParcelFileDescriptor
上使用C#reflection
我还没有尝试过,但如果Mono for Android甚至包装了Java类的私有成员,简单地使用C#reflection可能就足够了
如果失败,您可以继续进行JNI尝试。使用JNI应该可以: 未经测试的草图:
var methodId = JNIEnv.GetStaticMethodID(ParcelFileDescriptor.Class.Handle,
"createPipe",
"()[Landroid/os/ParcelFileDescriptor;");
var result = JNIEnv.CallStaticObjectMethod(myCSharpFileDescriptorInstance.Handle,
methodId);