Expose C# class through COM Interop

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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

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

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(); } 

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

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?

回答1:

 [ClassInterface(ClassInterfaceType.None)] 

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.

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.

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!