这篇文章与Visual Basic .NET 2010有关
所以,我想知道是否有任何方法可以从库中调用函数,例如System.ReadAllBytes通过字符串名称.
我一直在努力Assembly.GetExecutingAssembly().CreateInstance和System.Activator.CreateInstance追随CallByName(),但它们似乎都没有奏效.
我如何尝试它的示例:
Dim Inst As Object = Activator.CreateInstance("System.IO", False, New Object() {})
Dim Obj As Byte() = DirectCast(CallByName(Inst, "ReadAllBytes", CallType.Method, new object() {"C:\file.exe"}), Byte())
Run Code Online (Sandbox Code Playgroud)
帮助(一如既往)非常感谢
是的System.IO.File.ReadAllBytes(),你错过了"文件"部分.哪个是Shared方法,CallByName语句不够灵活,不允许调用此类方法.您将需要使用.NET中提供的更通用的Reflection.对于您的具体示例,这看起来像这样,为清楚起见,详细说明:
Imports System.Reflection
Module Module1
Sub Main()
Dim type = GetType(System.IO.File)
Dim method = type.GetMethod("ReadAllBytes")
Dim result = method.Invoke(Nothing, New Object() {"c:\temp\test.bin"})
Dim bytes = DirectCast(result, Byte())
End Sub
End Module
Run Code Online (Sandbox Code Playgroud)