Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
975 views
in Technique[技术] by (71.8m points)

.net - Expose C# class through COM Interop

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
 [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 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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...