
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
174
|
Chapter 3: Classes and Structures
See Also
See the “Main” and “Command-Line Arguments” topics in the MSDN documenta-
tion.
3.23 Retrofitting a Class to Interoperate with COM
Problem
An existing C# class needs to be usable by a COM object or will need to be usable
sometime in the future. You need to make your class work seamlessly with COM.
Solution
Microsoft has made COM interop quite easy. In fact, you really have to complete
only two minor steps to make your code visible to COM:
1. Set the Register for COM interop field in the project properties to
True. This pro-
duces a type library that can be used by a COM client.
2. Use the Regasm.exe command-line tool to register the class. For example, to reg-
ister the type library for the ClassLibrary1.dll, you would do the following:
regasm ClassLibrary1.dll /tlb:ClassLibrary1.tlb
By default, this tool will make many decisions for you. For example, new GUIDs are
created for your classes and interfaces unless you specify a particular GUID to use.
This can be a bad thing; it is usually a good idea to explicitly specify which GUIDs
your classes and interfaces are to use. To take control of how your C# code is viewed
and used from a COM client, you need to use a few attributes. Table 3-4 contains a
list of attributes and their descriptions that ...