简体   繁体   English

通过COM Interop公开C#类

[英]Expose C# class through COM Interop

I have a C# class library and also have a powerbuilder application. 我有一个C#类库,也有一个powerbuilder应用程序。 I want to expose the c# class and use it in the powerbuilder application. 我想公开c#类并在powerbuilder应用程序中使用它。 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 我使用以下博客介绍了这些功能,以便可以通过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

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. 因此,我公开了COM并使其可以在Powerbuilder中访问,但是我仍然有一些基本问题来确保是否遵循最佳指南。 The class looks before converting to COM looks like 该类在转换为COM之前看起来像

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

To convert to COM I created an interface and i wanted to expose only function1 and function2. 为了转换为COM,我创建了一个接口,我只想公开function1和function2。 So i modified the class as 所以我将类修改为

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

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. 在主类中,我进行了以下修改:1.由于我不想公开所有公共方法,因此我在AssemblyInfo中将COM visible属性设置为false。 2. Class looks like 2.类看起来像

[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}
}

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? 我有以下问题需要我更好地理解:1.如果我将class的visible属性设置为true以及默认的COM visible属性(在assemblyinfo)为假? 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? 我确实了解了如何通过复制dll并使用regasm.exe在客户端计算机中使用regasm进行部署,我的问题是如何在未安装.NET的非开发计算机中进行部署?

 [ClassInterface(ClassInterfaceType.None)]

That means that none of the class implementation details are visible, the proper and pure COM way. 这意味着没有任何类实现细节是可见的,即正确的纯COM方法。 So it is not necessary to apply [ComVisible(false)] on methods you don't want to expose. 因此, 没有必要申请[标记有ComVisible特性(假)上的方法,你不希望暴露。 Only the Iinterface methods are visible. Iinterface方法可见。

Using, say, ClassInterfaceType.AutoDual is a convenience in .NET, the CLR will synthesize an interface automatically. 例如,使用ClassInterfaceType.AutoDual在.NET中很方便,CLR会自动合成一个接口。 It matches the behavior of old versions of Visual Basic (VBA, VB6), they did not support interfaces yet. 它与Visual Basic的旧版本(VBA,VB6)的行为匹配,但它们尚不支持接口。 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. 但是,它确实暴露了太多信息,从System.Object继承的方法(如GetHashCode等)也将可见,而没有一种不错的方法来隐藏它们。 You also get a dependency on the mscorlib.tlb type library. 您还将获得对mscorlib.tlb类型库的依赖。 So declaring the interface explicitly like you did is the certainly better way. 因此,像您一样明确声明接口是肯定更好的方法。

The target machine must have .NET installed, rock-hard requirement. 目标计算机必须已安装.NET,严格要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM