Section 3.5. Interfaces in Visual Studio 2005


3.5. Interfaces in Visual Studio 2005

Visual Studio 2005 has excellent support for implementing and refactoring interfaces. As a component developer, your classes will occasionally need to implement an interface defined by another party. Instead of copying and pasting the interface definition, or typing it in, you can use Visual Studio 2005 to generate a skeletal implementation of the interface. A skeletal implementation is a do-nothing implementation: all implanted methods or properties throw an exception of type Exception and do not contain any other code. A skeletal implementation is required to at least get the code compiled as a starting point for your implementation of an interface, and it prevents clients from consuming a half-baked implementation. To have Visual Studio 2005 generate a skeletal interface implementation, you first add the interface to the class derivation chain. When you finish typing the interface name (such as IMyInterface), Visual Studio 2005 marks a little underscore tag under the I of the interface name. If you hover over IMyInterface, Visual Studio 2005 pops up a smart tag with a tool tip, "Options to implement interface." If you click the down arrow of the smart tip you can select from two options in the menu, implementing the interface either implicitly or explicitly (see Figure 3-2).

Figure 3-2. Using Visual Studio 2005 to provide a skeletal interface implementation


Once you select an option, Visual Studio 2005 creates a skeletal implementation of the interface on your class and scopes it with a collapsible #region directive. For example, consider this interface definition:

     public interface IMyInterface     {        void Method1(  );        int Method2(int number);        string Method3(  );     } 

If you select explicit implementation, Visual Studio 2005 generates this skeletal implementation:

     public class MyClass : IMyInterface     {        #region IMyInterface Members        void IMyInterface.Method1(  )        {           throw new Exception(  );        }        int IMyInterface.Method2(int number)        {           throw new Exception(  );        }        string IMyInterface.Method3(  )        {           throw new Exception(  );        }        #endregion     } 

Implementing a skeletal interface also works with generic interfaces.

Visual Basic 2005 has only IntelliSense-level skeletal interface implementation, and it doesn't generate a region around the skeleton.


3.5.1. Interface Refactoring

Code refactoring allows you to change the code structure without changing or affecting what the code itself actually does. Changing a variable name or packaging a few lines of code into a method are examples of code refactoring. Visual Studio 2005 contains a simple refactoring engine that enables several actions, including renaming types, variables, methods, or parameters; extracting a method out of a code section (and inserting a method call instead); encapsulating type members as properties; automating many formatting tasks; and auto-expanding common statements. The main difference between C# refactoring and doing a mere edit or find-and-replace is that you can harness the intelligence of the compiler to distinguish between code and comments, and so on.

In Visual Studio 2005, refactoring changes are limited to a single solution and do not propagate to client assemblies in different solutions.


You can invoke refactoring in two ways: you can select Refactor from the top-level Visual Studio 2005 menu, or you can select it from the pop-up context menu.

Of particular interest in the context of this chapter is the refactoring ability to extract an interface definition out of a set of public methods the type implements. For example, consider the following Calculator class:

     public abstract class Calculator     {        public int Add(int argument1,int argument2)        {           return argument1 + argument2;        }        public int Subtract(int argument1,int argument2)        {           return argument1 - argument2;        }        public virtual int Divide(int argument1,int argument2)        {           return argument1 + argument2;        }        public abstract int Multiply(int argument1,int argument2);     } 

To extract an interface out of the Calculator class, right-click anywhere inside the class definition and select Extract Interface... from the Refactor menu. This will bring up the Extract Interface dialog box, shown in Figure 3-3.

The dialog box will propose a name for the interface: the type's name, prefixed with an I. The refactoring will use the default (also called root) namespace of the project, as configured in the project settings.

Figure 3-3. The Extract Interface dialog box


The interface will be extracted to a separate file, which will automatically be added to the project. You can provide a filename in the dialog. Finally, all the public methods (or properties) of the type will be listed in the dialog, regardless of whether they are public, virtual, or abstract. Note that when a class hierarchy is involved, the refactoring engine will only include public methods explicitly declared by the class or overridden by it. To include the suggested methods in the new interface definition, you must explicitly check the checkboxes to the left of each method. After you click the OK button, the new interface will be placed in the specified new file, and Visual Studio 2005 will add the interface derivation to the Calculator class, as shown here:

     //In the file ICalculator.cs     interface ICalculator     {        int Add(int argument1,int argument2);        int Divide(int argument1,int argument2);        int Multiply(int argument1,int argument2);        int Subtruct(int argumentt argument2);     }         //In the file Calculator.cs     public abstract class Calculator : ICalculator     {...} 

Note that the extracted interface is internal, and that you have to explicitly make it public.

You can also extract one interface from the definition of another, in which case the new interface will be placed in a new file, but the original interface definition will not change (i.e., it will not inherit from the new interface). Visual Studio 2005 will not prefix the new interface name with an I, because that would result in a double II. Instead, it will append an ordinal number to the interface name.

Note that if the type already implements an interface implicitly, that interface's members will be included in the list in the Extract Interface dialog. Use explicit interface implementation on existing interfaces to avoid including them in the refactoring.



Programming. NET Components
Programming .NET Components, 2nd Edition
ISBN: 0596102070
EAN: 2147483647
Year: 2003
Pages: 145
Authors: Juval Lowy

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net