通过 COM 互操作公开 C# 类 [英] Expose C# class through COM Interop

查看:21
本文介绍了通过 COM 互操作公开 C# 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C# 类库,还有一个 powerbuilder 应用程序.我想公开 c# 类并在 powerbuilder 应用程序中使用它.我使用以下博客公开了这些功能,以便 powerbuilder 应用程序可以访问它https://whoisburiedhere.wordpress.com/2011/07/12/creating-a-com-object-from-scratch-with-c/http://jumbloid.blogspot.com/2009/12/making-net-dll-com-visible.html

I have a C# class library and also have a powerbuilder application. I want to expose the c# class and use it in the powerbuilder application. I used the following blogs to expose the functions so it can be accessed by powerbuilder application https://whoisburiedhere.wordpress.com/2011/07/12/creating-a-com-object-from-scratch-with-c/ http://jumbloid.blogspot.com/2009/12/making-net-dll-com-visible.html

所以我公开了 COM 并使其在 powerbuilder 中可访问,但我仍然有一些基本问题要确保我是否遵循最佳指南.该类在转换为 COM 之前看起来像

So i exposed the COM and made it accessible in powerbuilder but i still have some fundamental questions to make sure if i follow the best guidelines. The class looks before converting to COM looks like

 class classname
 {
    function1(){//do something}
    function2(){//do something}
    function3(){//do something}
    function4(){//do something}
 }  

为了转换为 COM,我创建了一个接口,我只想公开 function1 和 function2.所以我把这个类修改为

To convert to COM I created an interface and i wanted to expose only function1 and function2. So i modified the class as

[ComVisible(true)]
[Guid("03S3233DS-EBS2-5574-825F-EERSDG8999"),InterfaceType(ComInterfaceType.InterfaceIsDual)]
interface Iinterface
{
  function1();
  function2();
}

在主类中我做了以下修改1. 我在 AssemblyInfo 中将 COM 可见属性设置为 false,因为我不想公开所有公共方法.2.类的样子

In the main class i made the following modifications 1. I set the COM visible property to false in AssemblyInfo as i do not want to expose all the public methods. 2. Class looks like

[ComVisible(true)]
[Guid("2FD1574DS4-3FEA-455e-EW60A-EC1DFS54542D"), ClassInterface(ClassInterfaceType.None)]
class class1 : Iinterface
{
    function1(){//do something}
    function2(){//do something}
    [ComVisible(false)] //i don't want the mehtod to be exposed
    function3(){//do something}
    [ComVisible(false)]
    function4(){//do something}
}

我有以下问题需要我更好地理解1. 如果我将类的可见属性设置为 true 并将默认的 COM 可见属性(在 assemblyinfo 中)设置为 false,我是否明确将我不想公开的方法的 COM 可见属性设置为 false?我的理解是我只会有我想在界面中公开的功能,所以不管可见属性如何,如果我在界面中没有该功能,那么它将不可见?我确实了解如何通过复制 dll 并使用 regasm.exe 在客户端计算机中使用 regasm 进行部署,我的问题是如何在未安装 .NET 的非开发机器中进行部署?

I have the following questions for me to understand better 1. Do i explicitly set the COM visible property to false for the methods that i do not want to expose if i set the visible property of class to true and the default COM visible property (in assemblyinfo) to false? My understanding is i will only have functions that i want to expose in interface, so irrespective of the visible property, if i dont have the function in interface then it won't be visible? I did understand how to deploy using regasm in client computer by copying the dll and use regasm.exe, my question is how to deploy in non development machines with no .NET installed?

推荐答案

 [ClassInterface(ClassInterfaceType.None)]

这意味着没有任何类的实现细节是可见的,这是正确的纯 COM 方式.因此不需要在您不想公开的方法上应用 [ComVisible(false)].只有 Iinterface 方法可见.

That means that none of the class implementation details are visible, the proper and pure COM way. So it is not necessary to apply [ComVisible(false)] on methods you don't want to expose. Only the Iinterface methods are visible.

使用 ClassInterfaceType.AutoDual 是 .NET 中的一种便利,CLR 将自动合成一个接口.它与旧版本的 Visual Basic(VBA、VB6)的行为相匹配,它们还不支持接口.然而,它确实暴露了太多,从 System.Object 继承的方法(如 GetHashCode 等)也将是可见的,而没有一种体面的方式来隐藏它们.您还可以依赖 mscorlib.tlb 类型库.所以像你一样明确声明接口肯定是更好的方法.

Using, say, ClassInterfaceType.AutoDual is a convenience in .NET, the CLR will synthesize an interface automatically. It matches the behavior of old versions of Visual Basic (VBA, VB6), they did not support interfaces yet. It does however expose too much, the methods inherited from System.Object (like GetHashCode etc) will be visible as well without a decent way to hide them. You also get a dependency on the mscorlib.tlb type library. So declaring the interface explicitly like you did is the certainly better way.

目标机器必须安装了 .NET,非常严格的要求.

The target machine must have .NET installed, rock-hard requirement.

这篇关于通过 COM 互操作公开 C# 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆