【问题标题】:Building VB assembly in C# using VBCodeProvider().CompileAssemblyFromSource but cannot access functions inside VB code使用 VBCodeProvider().CompileAssemblyFromSource 在 C# 中构建 VB 程序集,但无法访问 VB 代码中的函数
【发布时间】:2013-10-08 05:34:36
【问题描述】:

VB代码:

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

C# 代码:

    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);

如您所见,其中只有一个模块和一个函数,那么为什么我对 GetMethods() 的调用不起作用?我对 VB 不是很熟悉,但它应该是一个静态方法,而且我认为它只是作为模块内的子程序编写的正确方法。

【问题讨论】:

  • 'Shared' 是 VB.Net 的'静态'

标签: c# .net vb.net dynamic code-generation


【解决方案1】:

想通了,需要使用 GetType() 而不是 GetModule():

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

作品:)

【讨论】:

    猜你喜欢
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多