Class.getName() for normal .NET classes

View: New views
4 Messages — Rating Filter:   Alert me  

Class.getName() for normal .NET classes

by Ted O'Connor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Is it possible to dynamically load a .NET class and get the real class
name back without having to check for and strip the "cli." prefix?

Here is my scenario:

String realName = "com.acme.MyClass";
Class clazz = Class.forName(realName);
String ikvmName = clazz.getName();

realName == ikvmName, when realName represents a class that was
written in Java but converted to .NET with ikvmc.

"cli." + realName == ikvmName, when realName represents a normal .NET
class (i.e. written in C#, VB.net, etc. like
"System.Data.SqlClient.SqlConnection")

It would be nice if the ikvmName was always the same as the realName
in this case.

Thanks.
Ted

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Ikvm-developers mailing list
Ikvm-developers@...
https://lists.sourceforge.net/lists/listinfo/ikvm-developers

Re: Class.getName() for normal .NET classes

by Jeroen Frijters :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ted O'Connor wrote:
> Is it possible to dynamically load a .NET class and get the real class
> name back without having to check for and strip the "cli." prefix?

You can use ikvm.runtime.Util.getInstanceTypeFromClass(clazz).get_TypeName().

(You can run ikvmstub on IKVM.OpenJDK.ClassLibrary.dll to get a jar that contains stubs for ikvm.runtime.Util.)

Regards,
Jeroen


-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Ikvm-developers mailing list
Ikvm-developers@...
https://lists.sourceforge.net/lists/listinfo/ikvm-developers

Re: Class.getName() for normal .NET classes

by Ted O'Connor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Jeroen.

I have code that might be running as Java or .NET so I didn't want to
introduce an ikvm.runtime.Util reference.  It is easy enough for me to
check for the "cli." prefix and handle it accordingly.  I was just
wondering if there was a better way or if this was a bug.

Ted


On Wed, May 7, 2008 at 12:03 PM, Jeroen Frijters <jeroen@...> wrote:

> Ted O'Connor wrote:
> > Is it possible to dynamically load a .NET class and get the real class
> > name back without having to check for and strip the "cli." prefix?
>
> You can use ikvm.runtime.Util.getInstanceTypeFromClass(clazz).get_TypeName().
>
> (You can run ikvmstub on IKVM.OpenJDK.ClassLibrary.dll to get a jar that contains stubs for ikvm.runtime.Util.)
>
> Regards,
> Jeroen
>
>

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Ikvm-developers mailing list
Ikvm-developers@...
https://lists.sourceforge.net/lists/listinfo/ikvm-developers

Re: Class.getName() for normal .NET classes

by Jeroen Frijters :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ted O'Connor wrote:
> I have code that might be running as Java or .NET so I didn't want to
> introduce an ikvm.runtime.Util reference.  It is easy enough for me to
> check for the "cli." prefix and handle it accordingly.  I was just
> wondering if there was a better way or if this was a bug.

There is no reason you can't have an ikvm.runtime.Util reference when running in Java, just make you don't actually call it when running on Java (or catch the NoClassDefFoundError).

Be aware that if you look for (and chop off) the "cli." prefix, that'll work most of the time, but not always. There is additional name mangling going on and it is also possible for a .NET type not to get the "cli." prefix (by adding the IKVM.Attributes.NoPackagePrefixAttribute to the type or assembly.

Regards,
Jeroen

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Ikvm-developers mailing list
Ikvm-developers@...
https://lists.sourceforge.net/lists/listinfo/ikvm-developers
LightInTheBox - Buy quality products at wholesale price