BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Refactoring
VS 2010+

Visual Studio 2010 Generate From Usage

Visual Studio 2010 includes a new code generation feature named Generate From Usage. This allows you to write code that references classes, structures, interfaces, enumerations and members that do not exist and have the IDE create them for you afterwards.

Generate From Usage

Some styles of programming, such as Test-driven Development (TDD), cause you to write code that references types or members that have are yet to be created. These elements are then created as "stubs" to allow compilation. The stubs are modified later to add the required functionality.

Visual Studio 2010 provides enhanced support for this style of development. This includes the "Generate From Usage" feature, which generates stubs for classes, structures, interfaces, methods, properties, fields, enumerations and enumeration constants.

Generating Classes

In a TDD approach, we may write a test for an undefined class. This may include the following code:

var shape = new Square();

When added to a new project, the "Square" element is underlined in red. This indicates that the code will not compile because "Square" is undefined. We can fix this with the Generate From Code function. To do so, right-click the undefined class name to show a context-sensitive menu. Open the Generate submenu and click Class and the class is generated in a new file within the current project. The red underline disappears to show that the problem has been resolved. The new file remains unopened to allow you to continue typing.

A second way to create the class is to use a smart tag. If you click the name of the class, a small underline symbol appears beneath its first letter. Pointing at the underline shows a smart tag icon, which can be expanded to show the available options. One of these is "Generate class for 'Square'". Clicking this option performs the same operation as above.

NB: If you prefer to use the keyboard, pressing Ctrl-. opens the smart tag options. You can press this key combination after typing the class name, avoiding the mouse for the entire operation.

The new class is generated in the project's default namespace and includes the using directives that are present for a manually created class. The code for the Square class is similar to that below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenerateFromUsage
{
    class Square
    {
    }
}

Generating Types

For more control over the created class, or to generate a structure, interface or enumeration, you can use the Generate Type command. For the previous "Square" symbol, you could choose "New Type..." from the Generate menu or "Generate new type..." from the smart tag options. Either command opens the Generate New Type dialog box:

Visual Studio 2010 Generate New Type Dialog Box

The dialog box provides several options:

  • Access. Determines the scope of the new item. If left as "Default", no access modifier is added to the class. You can specify the scope by selecting public or internal.
  • Kind. Specifies the kind of code to be created. You can create classes, structures, interfaces or enumerations.
  • Project. If you have multiple projects in the solution, choose which to add the code to. This is useful for TDD when tests are kept in a separate project to the application code.
  • File Name. Here you can specify that the new item should be added in a new file, which must be named, or an existing file, selected from a list.

Generating Properties and Fields

Properties and fields can be generated in a similar manner to types. If you add the following code the Size member will be underlined.

shape.Size = 20;

As this member does not include parentheses, it represents an undefined property or field. When you right-click to view the Generate menu, or show the smart tag, options are available for generating either of these. If you choose to create a property, the following code is added to the Square class. Note that the data type is inferred from the code. In many cases this type will be correct. Sometimes you will need to change it.

public int Size { get; set; }

Generating a field produces the following. As before the type is inferred and the member is public.

public int Size;
1 May 2010