通过COM Interop暴露C#类 [英] Expose C# class through COM Interop

查看:136
本文介绍了通过COM Interop暴露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. I在AssemblyInfo中将COM visible属性设置为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.如果我将类的visible属性设置为true,并且默认COM可见属性设置为true,那么我为我不想公开的方法显式设置COM visible属性为false在assemblyinfo)到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将自动合成一个接口。然而它暴露得太多了,继承自System.Object(如GetHashCode等)的方法也将是可见的,没有办法隐藏它们。

Using, say, ClassInterfaceType.AutoDual is a convenience in .NET, the CLR will synthesize an interface automatically. It however exposes too much, the methods inherited from System.Object (like GetHashCode etc) will be visible as well without a way to hide them. Declaring the interface explicitly like you did is the better way.

目标机必须安装.NET,硬盘需求。

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

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

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