Share via


Writing Type Converters

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies.
This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The Recipe Framework includes a number of library converter classes that you can use:

  • CodeIdentifierStringConverter. This class validates that the input value is a valid .NET identifier (for example, a valid method, property, or class name)
  • DteElementConverter. This class validates that the received value is a path expression that identifies an item in the logical tree view of the Solution Explorer, starting at the solution.
  • NamespaceStringConverter. This class validates that the input value is a valid namespace name.
  • ProjectConverter. This class validates that the input value is a path to a project, with the same rules as the DteElementConverter, but ensuring the target is always a project.
  • ProjectItemConverter. This class validates that the input value is a path to a project item, with the same rules as the DteElementConverter, but ensuring the target is always an item.
  • ReferenceConverter. This class validates that the input value is a path to a reference in a project.
  • RegexMatchStringConverter. This class validates that a string matches the pattern of a regular expression.
  • RegexNotMatchStringConverter. This class validates that a string does not match a pattern of a regular expression
  • SolutionConverter. This class validates that the input value is a reference to the root solution, that is, either a forward or a backward slash.
  • SolutionFolderConverter. This class validates that the input value is a path to a solution folder, with the same rules as the DteElementConverter, but ensuring the target is always a solution folder.

You may also want to write your own type converter. To define a type converter, you need to derive from the TypeConverter class, as shown in the following code example.

using System;
using System.ComponentModel;

namespace $PackageNamespace$.Converters
{
  /// <summary>
  /// Sample converter that returns a list of optional values to pick from, but 
  /// validates that the value entered starts with "Hello ".
  /// </summary>
  public class HelloWorldConverter : TypeConverter
  {
    static string[] validValues = new string[] {
    "Hello Guidance Automation Toolkit!", "Hello Microsoft!", "Hello World!" };

    public override bool IsValid(ITypeDescriptorContext context, object value)
    {
      return (value is string && ((string)value).StartsWith("Hello "));
    }

    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
      return false;
    }

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
      return true;
    }

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
      return new StandardValuesCollection(validValues);
    }
  }
}

Note

A type converter may use services from other components, including Visual Studio itself. For more information, see Obtaining and Using Host Services.

See also

Developing Wizards | Defining a Wizard | Defining a User Interface Value Editor | Writing a Custom Wizard Page | Implementing Argument Value Propagation