调用本地.NET (2) COM+来自 C# 应用程序的组件
我创建了一个 .NET 2 COM+ 组件,供我们古老的经典 ASP 网站使用。这在经典 ASP 中非常简单地完成...
Dim MenuManager : Set MenuManager = Server.CreateObject("MenuManager.MenuManager")
但是,我想使用 C# 控制台应用程序对该组件进行性能分析。
如何从 C# 控制台应用程序中调用它?
I've created a .NET 2 COM+ component to be consumed by our archaic classic ASP website. This is done quite simply in classic ASP...
Dim MenuManager : Set MenuManager = Server.CreateObject("MenuManager.MenuManager")
However, I want to performance profile the component using a C# console application.
How do I call this from within the C# console application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用 CreateObject() 函数的后期绑定。您想要窃取、乞求或借用 VS2010 的副本,以使用 C# 4.0 dynamic 关键字来轻松实现这一点。这是适用于任何机器的类似代码。它使用后期绑定来创建 FileSystemObject COM 组件,前两行相当于您的代码片段。它列出了 c:\windows 文件夹的子目录:
如果您不能使用 C# 版本 4,请考虑使用 VB.NET。它具有相同的语法,并且还具有 CreateObject() 函数。
You are using late binding with the CreateObject() function. You want to steal, beg or borrow a copy of VS2010 to use the C# 4.0 dynamic keyword to make that easy to do. Here's similar code that works on any machine. It uses late binding to create the FileSystemObject COM component, the first two lines are equivalent to your code snippet. It lists the subdirectories of the c:\windows folder:
If you can't use C# version 4 then consider using VB.NET instead. It has the same syntax and also has a CreateObject() function.
要从 C# 调用本机代码 COM 对象,您需要选择“添加引用”,然后选择“COM”选项卡并使用它添加对 COM 对象类型库的引用。这将运行 TLBIMP 来导入类型库并为 COM 对象生成一组很好的包装器和类型。然后,您只需使用这些类型和包装器来实例化 COM 对象并调用其方法。
这不适用于使用托管代码实现的 COM 对象。 TBIMP 工具注意到类型库是从托管代码生成的,并拒绝导入它。相反,它建议您只添加对程序集的 .NET 引用。
您也许能够手动构建一组 COM 包装器如此处所述< /a>.但是,当您可以使用 @Hans 建议的更简单的解决方案时,这似乎是一项艰巨的工作
To call a native code COM object from C# you'd select Add Reference and then select the COM tab and use it to add a reference to your COM object's type library. That would run TLBIMP to import your type library and generate a nice set of wrappers and types for the COM object. Then you would just use those types and wrappers to instantiate your COM object and call its methods.
This won't work for COM objects implemented using managed code. The TLBIMP tool notices that the type library was generated from managed code and refuses to import it. Instead it suggests that you just add a .NET reference to the assembly.
You might be able to build a set of COM wrappers by hand as described here. But that seems like an awful lot of work when you could use the much more straightforward solution suggested by @Hans