gpt4 book ai didi

c# - 使用 VBCodeProvider().CompileAssemblyFromSource 在 C# 中构建 VB 程序集但无法访问 VB 代码中的函数

转载 作者:行者123 更新时间:2023-11-30 12:29:14 26 4
gpt4 key购买 nike

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 不是很熟悉,但它应该是一个静态方法,而且我认为它的编写方式是正确的,它只是一个模块内的一个 sub。

最佳答案

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

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

作品:)

关于c# - 使用 VBCodeProvider().CompileAssemblyFromSource 在 C# 中构建 VB 程序集但无法访问 VB 代码中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19126680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com