Jni4net,如何定义在Java中使用的C#事件
我正在测试如何使用jni4net库从Java代码订阅C#events,但到目前为止,我制作的示例都没有成功。我试图在引发事件时发送一个Body()对象数组 C#代码: java端还没有使用C#事件给出的参数,但我只是想看看它是否被触发。但是,这会引发以下异常:Jni4net,如何定义在Java中使用的C#事件,java,c#,jni4net,Java,C#,Jni4net,我正在测试如何使用jni4net库从Java代码订阅C#events,但到目前为止,我制作的示例都没有成功。我试图在引发事件时发送一个Body()对象数组 C#代码: java端还没有使用C#事件给出的参数,但我只是想看看它是否被触发。但是,这会引发以下异常: Exception in thread "main" System.ArgumentNullException: Value cannot be null. Parameter name: method at System.
Exception in thread "main" System.ArgumentNullException: Value cannot be null.
Parameter name: method
at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure)
at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method)
at net.sf.jni4net.utils.Convertor.StrongJ2CpDelegate[TRes](JNIEnv env, JniLocalHandle obj)
at testlib.__LibEventHandler.UpdateBody0(IntPtr __envp, JniLocalHandle __obj, JniLocalHandle value)
at testlib.LibEventHandler.addUpdateBody(Native Method)
at testing.Start.<init>(Start.java:35)
at testing.Testing.main(Testing.java:24)
线程“main”系统中出现异常。ArgumentNullException:值不能为null。
参数名称:方法
在System.Delegate.CreateDelegate(类型类型、对象firstArgument、MethodInfo方法、布尔throwOnBindFailure)
在System.Delegate.CreateDelegate(类型类型,对象第一个参数,MethodInfo方法)
在net.sf.jni4net.utils.Convertor.StrongJ2CpDelegate[TRes](JNIEnv,JniLocalHandle obj)
在testlib.\uuuLibEventHandler.UpdateBody0(IntPtr\uuuuEnvp,JniLocalHandle\uuuuuuObj,JniLocalHandle值)
在testlib.LibEventHandler.addUpdateBody(本机方法)
在testing.Start.(Start.java:35)
at testing.testing.main(testing.java:24)
另一个网站(下面的链接)也讨论了同样的例外情况,但据我所知,我在定义事件时没有使用任何泛型。有人知道我哪里出错了吗
事实证明,JNI4NET不支持委托事件。因此,解决方案是在java端使用带有侦听器的接口事件系统。如果可能,您能分享您的解决方案或一些代码吗?我被困在同一个地方。
public Start(){
//initialise jni4net
try {
Bridge.setVerbose(true);
Bridge.init();
Bridge.LoadAndRegisterAssemblyFrom(new File("testlib.j4n.dll"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//create a instance of the library's main class and get the object that has all the events
LibMain lib = new LibMain();
LibEventHandler evHandler = lib.getEventHandler();
//subscribe to the event UpdateBody
evHandler.addUpdateBody(new EventHandler(){
@Override
public void Invoke(system.Object sender, EventArgs e){
Hello();
}
});
}
private void Hello(){
System.out.println("Hello World! triggered by c# event");
}
Exception in thread "main" System.ArgumentNullException: Value cannot be null.
Parameter name: method
at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure)
at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method)
at net.sf.jni4net.utils.Convertor.StrongJ2CpDelegate[TRes](JNIEnv env, JniLocalHandle obj)
at testlib.__LibEventHandler.UpdateBody0(IntPtr __envp, JniLocalHandle __obj, JniLocalHandle value)
at testlib.LibEventHandler.addUpdateBody(Native Method)
at testing.Start.<init>(Start.java:35)
at testing.Testing.main(Testing.java:24)