Creating a VB assembly in C # using VBCodeProvider (). CompileAssemblyFromSource, but does not have access to functions inside VB code

VB Code:

Public Module OnlyModule
    Public Sub OnlyFunction()
        'do stuff
    End Sub
End Module

C # code:

    Assembly vbAssembly = BuildAssembly(vbCode); //wrapper function, but returns valid, compiled vb assembly
    Module module = vbAssembly.GetModules()[0];
    MethodInfo method = module.GetMethods()[0]; //returns 0 methods!!
    method.Invoke(null, null);

As you can see, it has only one module and only one function, so why does my GetMethods () call not work? I am not completely familiar with VB, but it should be a static method, and I think that he wrote the correct path as soon as sub inside the module.

+4
source share
1 answer

It turned out that you need to use GetType (), not GetModule ():

Type type = vbAssembly.GetType("OnlyModule");
Method method = type.GetMethods()[0];

Work :)

+2
source

All Articles