enum from Java to C#

558 views
Skip to first unread message

Simon Chung

unread,
Sep 19, 2011, 2:07:52 PM9/19/11
to jni4net
Hello,

I have been trying to understand how to pass an enum from Java to C#
via the proxy. Everything else seem to work fine, and I can get an
enum from C# to Java

The code calls a C# API from Java and one of the parameters is an
enum. The proxy defines this enum parameter as a system.enum instead
of something like the real enum definition in C#.

So my question is how to get a System.enum to be something like what
is need by the C# API?

Furthmore, how to declare a System.enum to be used in this context?
The java constructor is protected.

Cheers,
Simon

Pavel Šavara

unread,
Sep 19, 2011, 3:27:00 PM9/19/11
to jni...@googlegroups.com
Hi Simon,

the enums are not implemented yet. The java enum works just because
they are public static fields on the type.
To define the C# enum type as a proxy, you have to wrap it using
proxygen. Then the proxygen could use the proxy type in proxy
parameters.
And then possibly you could use system.Enum.Parse(Type enumType, String value)

As I said, it's not implemented or tested well, so there will be bumps
on the road.

Pavel

> --
> 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/
>

Simon Chung

unread,
Sep 19, 2011, 3:49:26 PM9/19/11
to jni4net
Many thanks, Pavel. jni4net is certainly very useful to solve this
java - C# bridge problem. Much appreciated all the work you have done,
saved me a lot of time.

Could you elaborate a bit about "To define the C# enum type as a
proxy, you have to wrap it using proxygen."? I might take a look into
it.

Also Any idean what is the future plan / roadmap for jni4net?


Cheers,
Simon.

Pavel Šavara

unread,
Sep 19, 2011, 5:44:22 PM9/19/11
to jni...@googlegroups.com
> Could you elaborate a bit about "To define the C# enum type as a
> proxy, you have to wrap it using proxygen."? I might take a look into
> it.
You could generate a proxy for your enum. Something like this
http://code.google.com/p/jni4net/source/browse/trunk/jni4net.j/src/main/java/system/reflection/BindingFlags.java
http://code.google.com/p/jni4net/source/browse/trunk/jni4net.n/src/generated/system/reflection/BindingFlags.generated.cs

Note, there are no values to be used from Java side. But you could
hold strongly typed proxy of it, if it was created on CLR side for
you.
For example via system.Enum.Parse(Type enumType, String value)

> Also Any idean what is the future plan / roadmap for jni4net?

See the other email on this group.

Simon Chung

unread,
Sep 22, 2011, 1:07:31 PM9/22/11
to jni4net
Hello Pavel,

Your mention of the Parse method triggered an idea and I started
reading up and playing around with .NET and Java Enums about this
issue

There seems to be a work around without generating new proxies or
adding new wrappers.

The workaround is as follows:

1, get an instance of the .NET enum in Java using the existing proxy
getXXX() method.
2, Instantiate a new Java Object using the system.Enum.Parse(type,
String) method using the type of the Enum retrieved in step 1
3, Cast the new Java Object into system.Enum

and the new system.Enum can be used.

I have tried this very quickly in a simple test and seems OK. The hash
code returned are as expected.

Do you think it is OK?

Cheers,
Simon.



On Sep 19, 10:44 pm, Pavel Šavara <pavel.sav...@gmail.com> wrote:
> > Could you elaborate a bit about "To define the C# enum type as a
> > proxy, you have to wrap it using proxygen."? I might take a look into
> > it.
>
> You could generate a proxy for your enum. Something like thishttp://code.google.com/p/jni4net/source/browse/trunk/jni4net.j/src/ma...http://code.google.com/p/jni4net/source/browse/trunk/jni4net.n/src/ge...

Pavel Šavara

unread,
Sep 22, 2011, 2:30:50 PM9/22/11
to jni...@googlegroups.com
That's exactly what I proposed. :-)

day.trad...@gmail.com

unread,
Feb 7, 2013, 11:20:39 AM2/7/13
to jni...@googlegroups.com

Hi,

I try to apply the solution you have suggested. But I still have an issue with the " typeof" function that return " null".
here is a part of my code and below the error message.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public static void main(String arsg[]) throws IOException {
       
Bridge.init();
       
       Bridge.LoadAndRegisterAssemblyFrom(new java.io.File("E:/phillippe/wrapper/work/T4Definitions.40.dll"));
       Bridge.LoadAndRegisterAssemblyFrom(new java.io.File("E:/phillippe/wrapper/work/T4API.40.dll"));
       
       
Enum e = (Enum) Enum.Parse(APIServerType.typeof(),"Simulator");
 
 
 
 
 
      Host.Login(e, "T4Example", "112A04B0-5AAF-42F4-994E-FA7CB959C60B"); 
     
      
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Exception in thread "main" System.ArgumentNullException: La valeur ne peut pas être null.
Nom du paramètre : enumType
   à System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)
   à System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
   à System.__Enum.Parse19(IntPtr __envp, JniLocalHandle __class, JniLocalHandle enumType, JniLocalHandle value)
at system.Enum.Parse(Native Method)
at Main.main(Main.java:39)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Thank you for your help
Reply all
Reply to author
Forward
0 new messages