我正在尝试使用以下代码从 C# 调用 vb dll (com):
Type t = Type.GetTypeFromProgID("DLLName",true);
Object o = Activator.CreateInstance(t);
//object f = Activator.CreateInstance(z);
MethodInfo[] m = t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
但是我没有看到 DLL 中公开的方法。我已经尝试了各种 BindingFlags 组合,例如静态、公共(public)、非公共(public)、实例等。相反,我只是看到这些方法暴露了。谁能帮我确定为什么我看不到这些方法?谢谢。
- [0] {IntPtr GetIUnknown(Boolean ByRef)} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [1] {System.Object GetData(System.Object)} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [2] {Boolean SetData(System.Object, System.Object)} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [3] {Void ReleaseAllData()} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [4] {System.Object GetEventProvider(System.Type)} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [5] {Int32 ReleaseSelf()} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [6] {Void FinalReleaseSelf()} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [7] {System.Object CreateEventProvider(System.Type)} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [8] {IntPtr GetComIUnknown(Boolean)} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [9] {Boolean IsInstanceOfType(System.Type)} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [10] {System.Object InvokeMember(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Reflection.ParameterModifier[], System.Globalization.CultureInfo, System. String[])} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [11] {System.MarshalByRefObject MemberwiseClone(Boolean)} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [12] {System.Runtime.Remoting.ServerIdentity __RaceSetServerIdentity(System.Runtime.Remoting.ServerIdentity)} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [13] {Void __ResetServerIdentity()} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [14] {System.Object GetLifetimeService()} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [15] {System.Object InitializeLifetimeService()} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [16] {System.Runtime.Remoting.ObjRef CreateObjRef(System.Type)} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [17] {Boolean CanCastToXmlType(System.String, System.String)} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [18] {System.String ToString()} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [19] {Boolean Equals(System.Object)} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [20] {Int32 GetHashCode()} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [21] {System.Type GetType()} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [22] {Void Finalize()} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
- [23] {System.Object MemberwiseClone()} System.Reflection.MethodInfo {System.Reflection.RuntimeMethodInfo}
最佳答案
如果指定 COM ProgID,则从 GetTypeFromProgID 获得的类型实际上始终是内部 .NET Framework 类型 System.__ComObject
,用于实现 COM Interop Runtime Callable Wrapper,通过它管理代码可以使用非托管 COM 对象的方法。
您可以通过反射找到该类型的方法是该托管类型的托管方法,而不是包装的 COM 对象实现的非托管方法。所以你问题中列出的24个方法都是System.__ComObject
类型的方法。
您受 COM 机制的限制,无法找到有关 COM 对象的非托管 COM 方法的信息。通常,在使用 COM 时,您必须先了解要使用的接口(interface)及其要求,然后才能调用对象的任何方法。如果有关联的类型库,您可以从中获取有关方法的元数据,但如果没有,则您正在尝试做不可能的事情。
如果您能解释为什么您尝试通过发现 MethodInfo 对象来调用 COM 对象,而不是使用从 COM 服务器类型库生成的 Interop 程序集的常规方法,也许我们可以提供进一步的帮助。
关于c# - 从 C# 调用 VB DLL 上的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5901404/