gpt4 book ai didi

c# - 在不使用 Interop-DLL 的情况下在 Visual Studio C# 中使用 COM+ 对象

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:22 24 4
gpt4 key购买 nike

我正在编写使用另一个应用程序的程序。
假设应用程序 ProgID 是 TheCompany.TheProg

直到现在,我一直在使用“Add References/COM”并选择 TheProg Type Lib

但 3ed 方供应商提到不支持创建 Interop DLL,并且可能会随着版本升级导致一些接口(interface)更改。

我的问题是:如何在不创建 Interop DLL 的情况下引用此 TheCompany.TheProg COM+ 对象?

我知道我可以用

Type theProgType = Type.GetTypeFromProgID("TheCompany.TheProg");
dynamic myObject = Activator.CreateInstance(theProgType);
dynamic version = myObject.AMethod();

但是: 1. 我需要转换所有需要 .NET FW v4 的动态!
除非我想使用 theProgType.InvokeMethod() :)
2. 我没有 IntelliSense。

非常感谢

最佳答案

因为我刚刚处理过类似的事情,所以无法帮助自己对这个问题给出一个迟到的答案。

基本上,您可以定义自己的接口(interface)子集,仅通过后期绑定(bind)使用,方法如下:

[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] // late binding only
[Guid("00020400-0000-0000-C000-000000000046")] // IDispatch's GUID
interface IMyLateBindingAdaptor
{
// a subset of IWebBrowser2 [http://msdn.microsoft.com/en-us/library/aa752127(v=vs.85).aspx]
string LocationURL { get; }
void Navigate(string url, ref object flags, ref object TargetFrameName, ref object PostData, ref object Headers);
}

确保定义的属性和方法的名称和签名与供应商的规范相匹配。

使用它:

var adaptor = this.webBrowser1.ActiveXInstance as IMyLateBindingAdaptor;
if (null == adaptor)
throw new ApplicationException("No late binding.");
object missing = Type.Missing; // VT_EMPTY
adaptor.Navigate("http://www.example.com", ref missing, ref missing, ref missing, ref missing);
MessageBox.Show(adaptor.LocationURL);

在我看来,它比动态更好,因为它为您提供编译时类型安全(某种程度上)和 IntelliSense。

关于c# - 在不使用 Interop-DLL 的情况下在 Visual Studio C# 中使用 COM+ 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17672164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com