如何通过后期绑定从VB.Net调用位于C#DLL内的方法 [英] How to call a method located inside a C# DLL, from VB.Net, with late binding
问题描述
我无法掌握各种"Init","Accumulate" ...方法的工作方式,以允许从VB.Net代码调用位于DLL内的方法.
I can't get a grip on how the various "Init", "Accumulate" ... methods work, to enable calling a method located inside a DLL, from VB.Net code.
假设调用方法具有以下签名:
Let's say the method to call has the following signature :
public double ComputeMeanPosition(ref SortedList<DateTime, double> posByTime)
请问我如何使用该方法的实际示例,或者只是给我一些提示,以了解如何将参数实际传递给该方法,调用它并获取结果?
Would you please kindly point me to an actual example of the use of the methods, or simply give me a few hints as how to actually pass the parameters to the method, call it and fetch the results ?
@Olivier Jacot-Descombes:我肯定用名称空间名称作为类名称的前缀,但是仍然无法访问该对象.实际上,令您惊讶的是,您的建议没有涉及通过装入的DLL的内省显示的以下方法:
@Olivier Jacot-Descombes: I definitely prefix the class name by the namespace name, but yet fail to be able to reach the object. In fact I am surprised that your kind suggestion does not involve the following methods, displayed through introspection of the loaded 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()
编辑
事实上,我有一个类似于以下代码的代码,它可以成功地检查DLL并从中获取几种类型和方法,从而为我要调用的方法提供了以下结果.
In fact I have a code similar to the following one, that successfully manages to inspect the DLL and gets several types and methods out of it, giving the following results above for the method I would like to call.
这是代码
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方法以调用我选择的方法的地方,但这就是我遇到的问题……我可以吗?需要在调用对象之前先建立对象吗?我应该如何传递参数?如何获得结果?
At the end, it seems that the [[...]] is at the place where the Invoke method should be called to call the method of my choice, but that is where I'm stuck... Would I need to build an object before calling it ? How should I pass it the parameters ? How to get the result ?
推荐答案
类似的方法应该起作用:
Something like this should work:
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;
}
}
对于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
经过测试,此方法有效:
Tested and this worked:
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])
这篇关于如何通过后期绑定从VB.Net调用位于C#DLL内的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!