Free mag vol1 | Page 651

CHAPTER 15  TYPE REFLECTION, LATE BINDING, AND ATTRIBUTE-BASED PROGRAMMING The IAppFunctionality interface provides a polymorphic interface for all snap-ins that can be consumed by the extendable Windows Forms application. Given that this example is purely illustrative, you supply a single method named DoIt(). A more realistic interface (or a set of interfaces) might allow the object to generate scripting code, render an image onto the application’s toolbox, or integrate into the main menu of the hosting application. The CompanyInfoAttribute type is a custom attribute that can be applied on any class type that wishes to be snapped into the container. As you can tell by the definition of this class, [CompanyInfo] allows the developer of the snap-in to provide some basic details about the component’s point of origin. Building the C# Snap-In Next up, you need to create a type that implements the IAppFunctionality interface. Again, to focus on the overall design of an extendable application, a trivial type is in order. Assume a new C# Class Library project named CSharpSnapIn defines a class type named CSharpModule. Given that this class must make use of the types defined in CommonSnappableTypes, be sure to set a reference to the CommonSnappableTypes assembly (as well as System.Windows.Forms.dll to display a noteworthy message). This being said, here is the code: using using using using System; System.Collections.Generic; System.Linq; System.Text; using CommonSnappableTypes; using System.Windows.Forms; namespace CSharpSnapIn { [CompanyInfo(CompanyName = "My Company", CompanyUrl = "www.MyCompany.com")] public class CSharpModule : IAppFunctionality { void IAppFunctionality.DoIt() { MessageBox.Show("You have just used the C# snap-in!"); } } } Notice that I choose to make use of explicit interface implementation (see Chapter 9) when supporting the IAppFunctionality interface. This is not required; however, the idea is that the only part of the system that needs to directly interact with this interface type is the hosting Windows application. By explicitly implementing this interface, the DoIt() method is not directly exposed from the CSharpModule type. Building the Visual Basic Snap-In Now, to simulate the role of a third-party vendor who prefers Visual Basic over C#, create a new Visual Basic Class Library (VbSnapIn) that references the same external assemblies as the previous CSharpSnapIn project. 593