How to get Key and Value in Java from C# method which returns Object (which originally is Dictionary)?

277 views
Skip to first unread message

Yermek Kadyrbayev

unread,
Apr 19, 2016, 10:14:02 AM4/19/16
to jni4net
I managed to get List from system.Object using Java with following code:
List<UserInfo> userInfoList = castToList(api.UserGroupMemberList(newGroup));

public static <T extends List<?>> T castToList(Object obj) {
IEnumerator enumerator = ((IEnumerable) obj).GetEnumerator();
List<Object> list = new ArrayList<>();
while (enumerator.MoveNext()) {
list.add(enumerator.getCurrent());
}
return (T) list;

But I can't get it working with Dictionary, I guess it should be something like following:
    IDictionaryEnumerator enumerator = ((IDictionary) readMsg.getAttachments()).GetEnumerator();
    enumerator.MoveNext();
    enumerator.getCurrent();
    enumerator.getKey();
    enumerator.getValue();


Looks like returned Object is instance of __Enumerable class, and I can't cast it to something related to IDictionary/IDictionaryEnumerator((

Yermek Kadyrbayev

unread,
Apr 20, 2016, 8:58:28 AM4/20/16
to jni4net
I've tried Mihai Cadariu's branch (http://jni4net.googlecode.com/svn/branches/0-8-generics/distributable/), but it failed to generate proxies in my case.

вторник, 19 апреля 2016 г., 17:14:02 UTC+3 пользователь Yermek Kadyrbayev написал:
Message has been deleted

Yermek Kadyrbayev

unread,
Apr 20, 2016, 9:08:47 AM4/20/16
to jni4net


Here are respective proxies generated by jni4net 0.8.8.0:
    

@ClrMethod("()[[[LSystem/Collections/Generic/Dictionary`2;")
public native Object getAttachments();


and

methods.Add(global::net.sf.jni4net.jni.JNINativeMethod.Create(@__type, "getAttachments", "Attachments6", "()Lsystem/Object;"));

...


private static global::net.sf.jni4net.utils.JniHandle Attachments6(global::System.IntPtr @__envp, global::net.sf.jni4net.utils.JniLocalHandle @__obj) {
// ()Lsystem/Object;
// ()[[[LSystem/Collections/Generic/Dictionary`2;
global::net.sf.jni4net.jni.JNIEnv @__env = global::net.sf.jni4net.jni.JNIEnv.Wrap(@__envp);
global::net.sf.jni4net.utils.JniHandle @__return = default(global::net.sf.jni4net.utils.JniHandle);
try {
global::SomeLibrary.SomeClass @__real = global::net.sf.jni4net.utils.Convertor.StrongJp2C<global::SomeLibrary.SomeClass>(@__env, @__obj);
@__return = global::net.sf.jni4net.utils.Convertor.StrongC2Jp<global::System.Collections.Generic.Dictionary<string, object>>(@__env, @__real.Attachments);
}catch (global::System.Exception __ex){@__env.ThrowExisting(__ex);}
return @__return;
}

Yermek Kadyrbayev

unread,
May 4, 2016, 8:32:39 AM5/4/16
to jni4net
Solved by using Bridge.cast() method:

IDictionaryEnumerator enumerator = Bridge.cast(obj, IDictionary.class).GetEnumerator();

среда, 20 апреля 2016 г., 16:08:47 UTC+3 пользователь Yermek Kadyrbayev написал:

Pavel Šavara

unread,
May 4, 2016, 8:37:01 AM5/4/16
to jni4net
Nice, thanks for reporting back.

--
--
You received this message because you are subscribed to jni...@googlegroups.com
http://groups.google.com/group/jni4net?hl=en-GB?hl=en-GB
http://jni4net.sf.net/

---
You received this message because you are subscribed to the Google Groups "jni4net" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jni4net+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages