Question

I need to support a method named 'send-request' as an extension function to be used in an XSLT transformation. This is provided by an extension object on XslCompiledTransform. The cool thing about extension objects vs. <msxsl:script> is, well, that I don't have to use <msxsl:script>, you just declare the namespace and call the function. The bad thing is that the function name must match exactly the CIL method name.

So, I'm wondering, is there a .NET language that supports hyphens in method names ? CLS compliance is not required, the method is invoked using reflection.

Or, can I use some technique that modifies the IL of an assembly to change the method name ?

Or, is there a way to intercept the reflection GetMethod call and trick the caller that a method 'send-request' exists, but return a 'SendRequest' method instead ?

Was it helpful?

Solution

You can do this using reflection.

I compiled the following into an assembly using ILAsm (I cut out all the fluff necessary to make this actually compile; these are just the salient bits (pun intended)):

.method private hidebysig static int32 
'Te-st'() cil managed {
    // Code size       8 (0x8)
    .maxstack  1
    .locals init ([0] int32 CS$1$0000)
    IL_0000:  nop
    IL_0001:  ldc.i4.s 0x11
    IL_0002:  stloc.0
    IL_0003:  br.s       IL_0005

    IL_0005:  ldloc.0
    IL_0006:  ret
}

I defined this method in a class named Program in a namespace named Test. Note that the method name is surrounded by single quotes. This is per the spec (ECMA #335):

Identifiers are used to name entities. Simple identifiers are equivalent to an ID. However, the ILAsm syntax allows the use of any identifier that can be formed using the Unicode character set (see Partition I). To achieve this, an identifier shall be placed within single quotation marks.

Then:

var assembly = Assembly.LoadFrom(path);
var method = assembly.GetType("Test.Program")
                     .GetMethod(
                         "Te-st",
                         BindingFlags.Static | BindingFlags.NonPublic
             );
Console.WriteLine(method.Invoke(null, null));

This produced output:

17

I doubt that is the possible in the usual .NET languages. I don't know of any languages that allow/require you to "escape" identifiers. Without that, could you imagine trying to disambiguate the following:

int a;
int b;
int a-b;
int diff = a-b; // is that a minus b or the variable a-b?

Maybe there's a COBOL.NET where you could do this, I don't know.

OTHER TIPS

Hyphens in identifier names are pretty common in Lisp and Scheme, and thus I suspect it is natively supported in IronScheme, at least. It's also pretty easy to create methods like this in Ruby, despite the fact that hypens are actually illegal in Ruby identifiers, so you could use IronRuby.

Also, there's the @ escape-thingy in C# that allows you to use otherwise illegal identifiers, but I couldn't quite figure out how to use it for this case.

I think the simplest thing you could do here is define the method directly in IL. First define an abstract class which has every other method you want. Then define a derived class in IL which contains the hyphen'd method name and compile it into a separate library.

I'm reasonably sure that part of the spec for CLR languages is that they must be able to handle any name that the runtime supports, even if it happens to be a reserved keyword in that language or otherwise illegal.

Assuming that's the case, if the runtime supports hyphens in method names, then all CLR languages should be able to handle them, even if it requires something like e.g. VB.NET's [] syntax to declare or use them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top