9

I'm trying to use a DLL generated by ikvmc from a jar file compiled from Scala code (yeah my day is THAT great). The Scala compiler seems to generate identifiers containing dollar signs for operator overloads, and IKVM uses those in the generated DLL (I can see it in Reflector). The problem is, dollar signs are illegal in C# code, and so I can't reference those methods.

Any way to work around this problem?

Martin
  • 224
  • 2
  • 9

4 Answers4

6

You should be able to access the funky methods using reflection. Not a nice solution, but at least it should work. Depending on the structure of the API in the DLL it may be feasible to create a wrapper around the methods to localise the reflection code. Then from the rest of your code just call the nice wrapper.

The alternative would be to hack on the IL in the target DLL and change the identifiers. Or do some post-build IL-hacking on your own code.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
4

Perhaps you can teach IKVM to rename these identifiers such that they have no dollar sign? I'm not super familar, but a quick search pointed me at these:

http://weblog.ikvm.net/default.aspx?date=2005-05-02

What is the format of the Remap XML file for IKVM?

String and complex data types in Map.xml for IKVM!

Good Hunting

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
1

Write synonyms for those methods:

def +(a:A,b:A) = a + b
val plus = + _
pedrofurla
  • 12,763
  • 1
  • 38
  • 49
  • 1
    Would work, but I'm primarily interested in the methods exposed by the collection classes. I don't want to change all the code to use a special version of those. I'll probably end up creating wrappers in a special Scala class that I'll then use from C#. – Martin Oct 09 '12 at 23:44
1

I fear that you will have to use Reflection in order to access those members. Escaping simply doesn't work in your case.


But for thoose of you, who interested in escaping mechanics I've wrote an explanation.

In C# you can use the @-sign in order to escape keywords and use them as identifiers. However, this does not help to escape invalid characters:

bool @bool = false;

There is a way to write identifiers differently by using a Unicode escape sequence:

int i\u0064;  // '\u0064' == 'd'  
id = 5;

Yes this works. However, even with this trick you can still not use the $-sign in an identifier. Trying...

int i\u0024;  // '\u0024' == '$'  

... gives the compiler error "Unexpected character '\u0024'". The identifier must still be a valid identifier! The c# compiler probably resolves the escape sequence in a kind of pre-processing and treats the resulting identifier as if it had been entered normally

So what is this escaping good for? Maybe it can help you, if someone uses a foreign language character that is not on your keyboard.

int \u00E4; // German a-Umlaut
ä = 5;

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188