如何使用后期绑定从VB.Net调用位于C#DLL内的方法

时间:2012-05-04 14:36:01

标签: c# .net vb.net late-binding

我无法控制各种“Init”,“Accumulate”...方法如何工作,以便从VB.Net代码中调用位于DLL中的方法。

假设要调用的方法具有以下签名:

public double ComputeMeanPosition(ref SortedList<DateTime, double> posByTime)

请您指出使用这些方法的实际示例,或者只是给我一些提示,如何将参数实际传递给方法,调用它并获取结果?

@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()

修改

实际上我有一个类似于下面的代码,它成功地设法检查DLL并从中获取了几种类型和方法,给出了我想要调用的方法的上述结果。

这是代码

        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方法来调用我选择的方法的地方,但那就是我被卡住的地方......我会不会需要在调用它之前构建一个对象?我应该如何传递参数?如何获得结果?

3 个答案:

答案 0 :(得分:5)

这样的事情应该有效:

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

经过测试并且有效:

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

答案 1 :(得分:2)

这不是实际的代码,因为我不是坐在实际的c#编辑器前面,而是这样的

// using System.Reflection required

// first load the external assembly into the appdomain and 
// create an instance of the object
var assembly = Assembly.Load("Path to the Assembly");
var type = assembley.GetType("TheNameOfTheClass");
var ctor = type.GetConstuctor();
var object = ctor.Invoke(); // assuming an empty construtor, else you'll need to pass in data

// next invoke the method
var methodInfo = assembly.GetMethodByName("ComputeMeanPosition");
var param = new SortedList<DateTime, double>();
var result = methodInfo.Invoke(object, new object[] { param });

答案 2 :(得分:1)

您可以像在VB

中一样调用此方法
Public Function ComputeMeanPosition( _
    ByRef posByTime As SortedList(Of DateTime, Double)) As Double

注意:我使用此在线snippet converter将C#代码转换为VB。在转换之前,您必须在C#方法标题中添加一个空方法体{}

public double ComputeMeanPosition(ref SortedList<DateTime, double> posByTime) {}

由于此方法未在C#中声明为static(VB中为Shared),因此首先需要创建一个对象。该方法很可能在类中声明。假设这个类被命名为“MyClass”,那么你必须写这样的东西

Dim posByTime = New SortedList(Of DateTime, Double)()
Dim obj = New MyClass()
Dim result As Double = obj.ComputeMeanPosition(posByTime)

如果构造函数(New方法)声明参数,则必须在创建对象时传递它们

Dim obj = New MyClass(arg1, arg2, ...)

根据ComputeMeanPosition期望的内容,您必须在调用之前将项目添加到列表posByTime


如果该方法在C#中声明为static(VB中为Shared),则应使用类名限定它,而不是创建对象。

Dim posByTime = New SortedList(Of DateTime, Double)()
Dim result As Double = MyClass.ComputeMeanPosition(posByTime)

<强>更新

如果你动态加载程序集,你可能会做这样的事情

Dim ass As Assembly = Assembly.LoadFrom("C:\SomePath\MyDll.dll")
Dim obj As Object = ass.CreateInstance("MyClass", True) 

Dim posByTime = New SortedList(Of DateTime, Double)()
Dim result As Double = obj.ComputeMeanPosition(posByTime)

您必须为后期绑定设置Option Strict Off


如果构造函数需要参数,则必须将它们传递给CreateInstance

的另一个重载
Dim args = new Object() { arg1, arg2, ... }
Dim obj As Object = ass.CreateInstance("MyClass", True, _
    BindingFlags.Public Or BindingFlags.Instance, Nothing, Nothing, _
    args, Nothing, Nothing)