Search This Blog

Tuesday, March 5, 2013

ComVisibleAttribute C#

C# > InteropServices > ComVisible

ComVisible Attribute indicates that the managed type is visible to COM.
The default value is true.

Examples:


1. AssemblyInfo.cs
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
     [assembly: ComVisible(false)]

2.ComVisible in classes and methods
   [ComVisible(false)] //the class and its members are invisible to COM
   class ClassA
   {
        public ClassA()
        {
        }
        //if you derive ClassB from ClassA and export ClassB to COM, ClassA Method1 become visible to COM
        public int Method1()
        {
            return 0;
        }
        [ComVisible(false)] // Method2 is invisible to COM
        public int Method2()
        {
            return 1; 
        }
   }