There are two COM related assembly attributes in the auto-generated AssemblyInfo source file of C#, VB.NET or C++/CLI projects. They are ComVisible and typelib Guid.
The ComVisible indicates whether the types in the resultant assembly will be visible or not to COM clients. If it is specified as false, even if the assembly is registered as a COM server through the COM interop, no types of the assembly will be exposed.
Here is a sample use:
[assembly: ComVisible(false)]
In fact, it will end up either the COM server not being really registered or not being shown up in the Add Reference UI of the Visual Studio IDE. So this assembly-level attribute does not seem to make much sense.
If we really want to give it a try, e.g. checking off the ‘Register for COM interop’ option in the Assembly Properties dialog and still keeping the assembly level ComVisible attribute as the default ‘false’, a warning message will be generated when the project is being built.
"C:\Temp\ComServerTest\bin\Debug\ComServerTest.dll" does not contain any types that can be registered for COM Interop.”
In fact, the attribute can also apply to type level to indicate whether the type is visible to the COM world or not. That really makes sense to me.
Here is a sample use for the class level ComVisible attribute:
…
using System.Runtime.InteropServices;
namespace ComServerTest
{
[ComVisible(true)]
[Guid("0EB16A37-447F-403c-A5A2-804EF32494DC")]
public class Class1
{
…
If a COM class is defined as above, the assembly level ComVisible is specified as true:
[assembly: ComVisible(true)]
and the ‘Register for COM interop’ option is turned on from bottom of the Build tab of the Project Properties UI, the ComServerTest COM server will appear in the COM component list of the Add Reference UI:
Now, let us look at the assembly level Guid attribute. If it is set, the GUID will be used to identify the COM typelib when the assembly is exposed to COM through COM interop:
[assembly: Guid("baece144-c49e-427d-94c4-77a3401b4221")]
It should be optional. I am not sure what will exactly happen if the assembly Guid attribute is omitted from the AssemblyInfo source. Probably, a typelib GUID will still be generated and written into the Windows registry though we have to dig into the TypeLib trees of the registry to sort it out when it is needed. And I am not sure either whether the auto-generated typelib GUID will change along with every build/registration. Most likely will it behave so, I believe. If readers are interested, please feel free to give it a try.
A Guid attribute can also be defined on the interface and on the class. These values are used by COM clients to locate the assembly that implements the COM interface or class.
In terms of the class level Guid attribute, we have demonstrated in some example code a bit earlier.
AssemblyInfo Updater of RevitAddinWidget can help review popular assembly information of all available projects in a Visual Studio solution and update the information conveniently and flexibly in a single central place.
Recent Comments