调用本地.NET (2) COM+来自 C# 应用程序的组件

发布于 2024-11-14 17:11:57 字数 242 浏览 2 评论 0原文

我创建了一个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两个我 2024-11-21 17:11:57

您正在使用 CreateObject() 函数的后期绑定。您想要窃取、乞求或借用 VS2010 的副本,以使用 C# 4.0 dynamic 关键字来轻松实现这一点。这是适用于任何机器的类似代码。它使用后期绑定来创建 FileSystemObject COM 组件,前两行相当于您的代码片段。它列出了 c:\windows 文件夹的子目录:

using System;

class Program {
    static void Main(string[] args) {
        var type = Type.GetTypeFromProgID("Scripting.FileSystemObject");
        dynamic obj = Activator.CreateInstance(type);
        dynamic win = obj.GetFolder("c:/windows");
        foreach (dynamic subwin in win.SubFolders) {
            Console.WriteLine(subwin.Name);
        }
        Console.ReadLine();
    }
}

如果您不能使用 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:

using System;

class Program {
    static void Main(string[] args) {
        var type = Type.GetTypeFromProgID("Scripting.FileSystemObject");
        dynamic obj = Activator.CreateInstance(type);
        dynamic win = obj.GetFolder("c:/windows");
        foreach (dynamic subwin in win.SubFolders) {
            Console.WriteLine(subwin.Name);
        }
        Console.ReadLine();
    }
}

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.

笔落惊风雨 2024-11-21 17:11:57

要从 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文