从 C# 调用 VB DLL 上的方法
我正在尝试使用以下代码从 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 组合,例如静态、公共、非公共、实例等。相反,我只看到这些方法公开。谁能帮助我确定为什么我看不到这些方法?谢谢。
- [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}
I am trying to call a vb dll (com) from C# using the following code:
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);
However i am not seeing the methods which are exposed in the DLL. I have tried all sorts of BindingFlags combinations such as static, public, nonpublic, instance, etc. Instead I just see these methods exposed. Can anyone help me identify why I am not able to see the methods? Thanks.
- [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}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果指定 COM ProgID,则从 GetTypeFromProgID 获取的 Type 实际上始终是内部 .NET Framework 类型
System.__ComObject
,它用于实现 COM Interop Runtime Callable Wrapper,托管代码可以通过该包装器使用非托管 COM 对象的方法。您可以通过反射找到该类型的方法,这些方法是该托管类型的托管方法,而不是由包装的 COM 对象实现的非托管方法。所以你的问题中列出的24个方法是
System.__ComObject
类型的方法。您无法找到有关 COM 对象的非托管 COM 方法的信息,但受到 COM 机制的限制。一般来说,在使用 COM 时,您必须知道要使用什么接口及其要求,然后才能调用对象上的任何方法。如果有关联的类型库,您就可以在其中获取方法的元数据,但如果没有,您就是在尝试做不可能的事情。
如果您可以解释为什么尝试通过发现 MethodInfo 对象来调用 COM 对象,而不是使用从 COM 服务器的类型库生成的 Interop 程序集的常规方法,也许我们可以提供更多帮助。
The Type which is obtained from GetTypeFromProgID if you specify a COM ProgID is actually always the internal .NET Framework type
System.__ComObject
, which is used to implement the COM Interop Runtime Callable Wrapper through which managed code can consume the unmanaged COM object's methods.The methods of this Type which you can find out about by Reflection, are the managed methods of this managed type, not the unmanaged methods implemented by the wrapped COM object. So the 24 methods listed in your question are the methods of the
System.__ComObject
type.You are limited by the mechanics of COM as to what you can find out about the unmanaged COM methods of a COM object. In general, when using COM you have to know what interface you want to use, and what its requirements are, before you can call any methods on an object. If there is an associated type library, that is where you can get metadata on the methods, but if there isn't, you are trying to do the impossible.
If you could explain why you are trying to call the COM object by discovering MethodInfo objects, rather than using the normal approach of an Interop assembly generated from the COM server's type library, perhaps we could help further.