Ski*_*tol 1 .net c# vb.net late-binding
我无法控制各种"Init","Accumulate"......方法如何工作,以便从VB.Net代码中调用位于DLL中的方法.
假设要调用的方法具有以下签名:
public double ComputeMeanPosition(ref SortedList<DateTime, double> posByTime)
Run Code Online (Sandbox Code Playgroud)
请您指出使用这些方法的实际示例,或者只是给我一些提示,如何将参数实际传递给方法,调用它并获取结果?
@Olivier Jacot-Descombes:我确实用名称空间名称为类名添加前缀,但却无法访问该对象.事实上,我很惊讶您的善意建议不涉及以下方法,通过内加加载的DLL显示:
Type: MyClassName
Method: Void Init()
Method: Void Accumulate(System.Data.SqlTypes.SqlDouble, System.Data.SqlTypes.SqlDateTime, System.Data.SqlTypes.SqlBoolean)
Method: Void Merge(MyClassName)
Method: System.Data.SqlTypes.SqlDouble Terminate()
Method: Void Write(System.IO.BinaryWriter)
Method: Void Read(System.IO.BinaryReader)
Method: Boolean Equals(System.Object)
Method: Int32 GetHashCode()
Method: System.String ToString()
Method: System.Type GetType()
Run Code Online (Sandbox Code Playgroud)
编辑
实际上我有一个类似于下面的代码,它成功地设法检查DLL并从中获取了几种类型和方法,给出了我想要调用的方法的上述结果.
这是代码
Run Code Online (Sandbox Code Playgroud)For Each oneModule As Reflection.Module In useAssembly.GetLoadedModules() Console.WriteLine(" - " & oneModule.Name) For Each oneType As System.Type In oneModule.GetTypes() Console.WriteLine(" Type: " & oneType.Name) For Each oneField As Reflection.FieldInfo In oneType.GetFields() Console.WriteLine(" Field: " & oneField.ToString()) Next oneField For Each oneMethod As Reflection.MethodInfo In oneType.GetMethods() Console.WriteLine(" Method: " & oneMethod.ToString()) [[ ADD "Invoke" here ?]] Next oneMethod Next oneType Next oneModule
最后,似乎[[...]]位于应该调用Invoke方法来调用我选择的方法的地方,但这就是我被困住的地方......我是否需要构建调用它之前的一个对象?我应该如何传递参数?如何获得结果?
这样的事情应该有效:
using System;
using System.Reflection;
using System.IO;
public class MainClass
{
public static int Main(string[] args)
{
Assembly a = null;
try
{
a = Assembly.Load("YourLibraryName");
}
catch(FileNotFoundException e)
{Console.WriteLine(e.Message);}
Type classType = a.GetType("YourLibraryName.ClassName");
object obj = Activator.CreateInstance(classType);
object[] paramArray = new object[1];
paramArray[0] = new SortledList<DateTime, double>();
MethodInfo mi = classType.GetMethod("ComputeMeanPosition");
mi.Invoke(obj, paramArray);
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
对于VB.NET:
Imports System
Imports System.Reflection
Imports System.IO
Public Class MainClass
Public Shared Function Main(args As String()) As Integer
Dim a As Assembly = Nothing
Try
a = Assembly.Load("YourLibraryName")
Catch e As FileNotFoundException
Console.WriteLine(e.Message)
End Try
Dim classType As Type = a.[GetType]("YourLibraryName.ClassName")
Dim obj As Object = Activator.CreateInstance(classType)
Dim [paramArray] As Object() = New Object(0) {}
[paramArray](0) = New SortledList(Of DateTime, Double)()
Dim mi As MethodInfo = classType.GetMethod("ComputeMeanPosition")
mi.Invoke(obj, [paramArray])
Return 0
End Function
End Class
Run Code Online (Sandbox Code Playgroud)
经测试,这有效:
Dim a As Assembly = Nothing
Try
a = Assembly.Load("TestLib")
Catch ex As FileNotFoundException
Console.WriteLine(ex.Message)
End Try
Dim classType As Type = a.[GetType]("TestLib.Class1")
Dim obj As Object = Activator.CreateInstance(classType)
Dim [paramArray] As Object() = New Object(0) {}
[paramArray](0) = New SortedList(Of DateTime, Double)()
Dim mi As MethodInfo = classType.GetMethod("ComputeMeanPosition")
mi.Invoke(obj, [paramArray])
Run Code Online (Sandbox Code Playgroud)