Search code examples
c#clrikvm

Call method with special characters in name from C#


When using ikvmc to compile JAR to DLL, it generates methods and classes with funny names, like

TestClass.__<clinit>(object X);

or

TestClass$1.MethodName();

I wish to call and/or override them from the C# client. How can I achieve this without using Reflection?

Update: Not only call, but override in inherited classes too...


Solution

  • You must use reflection at least for creation of delegate:

    var assembly = typeof(SomeJarToDllAssembly.SomeType).Assembly;
    var type_TestClass1 = assembly.GetType("TestClass$1");
    var method_clinit = type_TestClass.GetMethod("__<clinit>");
    var dlgClinit = (Action<object>)Delegate.Create(type_TestClass, method_clinit);
    
    // call delegate like normal method (it's fast as normal method calling)
    dlgClinit(new object());
    

    There are several cases how correctly create delegates. See the MSDN reference guide: System.Delegate and the CreateDelegate methods.