Click here to Skip to main content
15,513,143 members
Articles / Programming Languages / C#
Technical Blog
Posted 28 Jun 2013

Stats

76K views
39 bookmarked

Best Practice in Writing a COM-Visible Assembly (C#)

Rate me:
Please Sign up or sign in to vote.
4.93/5 (17 votes)
5 Mar 2014CPOL1 min read
I summarized some best practice rules in writing a COM-visible assembly according to my recent work.

Introduction

I summarized some best practice rules in writing a COM-visible assembly according to my recent work.

  1. In AssemblyInfo.cs, I wrote two attributes:
    C#
    [assembly: ComVisible(false)]
    [assembly: Guid("64729ced-7a8d-4c90-af7f-a41725cfe216")] 

    They indicated I didn’t want all public classes to be COM-visible and I didn’t want Visual Studio to implicitly generate a random GUID as my library ID.

  2. I unchecked the “Register for COM interop” box in the project’s Build option because I didn’t want Visual Studio to register the assembly on the build machine.
  3. I wrote each COM-visible class like the following example:
    C#
    [ComVisible(true)]
    [Guid("7884998B-0504-4CBE-9FF9-6BBAA9776188")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("MyNamespace.MyClass")]
    public class MyClass : IMyClass
    {
    } 

    Here, I explicitly declared the class as COM-visible and gave it a GUID as the class ID. Visual Studio would generate a random GUID if I didn’t specify one. I used ClassInterfaceType.None to prevent Visual Studio from generating an interface for the class automatically. The automatic behavior would expose methods which I didn’t want to expose and could introduce unwanted method declarations.

    By the way, you can explicitly specify the ProgId attribute. By default, namespaces and class name will be combined as the ProgId of the class.

  4. I declared an interface for each COM-visible class explicitly like the following example:
    C#
    [ComVisible(true)]
    [Guid("67F6AA4C-A9A5-4682-98F9-15BDF2246A74")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IMyClass
    {
    }

    There was no special rule for InterfaceType. It would depend on application needs. And I only declared the methods which I wanted to expose in the interface.

This article was originally posted at http://blog.csdn.net/pkudzy/article/details/9183637

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
China China
Over 10-years experience in using Microsoft technologies.
At present, working as the architect of a clustered real-time data delivery and visualization system, responsible for the design of component architecture, product packaging and deployment, also targeting private cloud solutions for future.

Comments and Discussions

 
QuestionKeyword Public may be obvious but super-important Pin
Member 1307519826-Oct-17 10:25
Member 1307519826-Oct-17 10:25 
GeneralMy vote of 5 Pin
Joezer BH23-Jul-15 20:11
professionalJoezer BH23-Jul-15 20:11 
SuggestionSuper useful Pin
Neoncyber200118-Nov-14 20:18
professionalNeoncyber200118-Nov-14 20:18 
GeneralExcellent Article! Pin
Member 1112972424-Oct-14 13:46
Member 1112972424-Oct-14 13:46 
QuestionLooks like your information is incomplete Pin
feraudy13-Apr-14 10:44
feraudy13-Apr-14 10:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.