The AddMainClass Recipe

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.

One of the most interesting recipes that is defined when you create a new Guidance Package from the Guidance Package Development template is the AddMainClass recipe. This recipe is used to create a new class in a project.

The recipe specifies a number of arguments, and in some cases, specifies value providers for them. It also uses the <MonitorArgument> element to ensure that the TargetFile argument monitors the ClassName argument, and it uses the evaluator value provider to ensure that the value of the TargetFile argument is derived from the value of the ClassName argument by adding ".cs" to the end of it.

The <GatheringServiceData> element defines the pages of a wizard to collect information from the user about the class name, target namespace, and a message; then the <Actions> element is used to specify the AddMainClass action. For more information about the AddMainClass action, see Writing Action Classes.

The recipe definition is shown in the following XML code example.

<Recipe Name="AddMainClass" Recurrent="false">
  <Types>
    <TypeAlias Name="Evaluator" Type="Microsoft.Practices.RecipeFramework.Library.ValueProviders.ExpressionEvaluatorValueProvider,Microsoft.Practices.RecipeFramework.Library" />
  </Types>
  <Caption>Add class with Main method</Caption>
    <HostData>
      <Icon ID="630" />
      <CommandBar Name="Project" />
    </HostData>
    <Arguments>
      <Argument Name="CurrentProject" Type="EnvDTE.Project, EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
        <ValueProvider Type="Microsoft.Practices.RecipeFramework.Library.ValueProviders.FirstSelectedProject, Microsoft.Practices.RecipeFramework.Library" />
      </Argument>
      <Argument Name="ClassName">
        <Converter Type="Microsoft.Practices.RecipeFramework.Library.Converters.CodeIdentifierStringConverter, Microsoft.Practices.RecipeFramework.Library"/>
      </Argument>
      <Argument Name="TargetFile">
        <ValueProvider Type="Evaluator" Expression="$(ClassName).cs">
          <MonitorArgument Name="ClassName" />
        </ValueProvider>
      </Argument>
      <Argument Name="TargetNamespace">
        <Converter Type="Microsoft.Practices.RecipeFramework.Library.Converters.NamespaceStringConverter, Microsoft.Practices.RecipeFramework.Library"/>
        <ValueProvider Type="Evaluator" Expression="$(CurrentProject.Properties.Item('DefaultNamespace').Value)" />
      </Argument>
      <Argument Name="HelloMessage">
        <Converter Type="PackageNamespace.Converters.HelloWorldConverter, PackageName" />
      </Argument>
    </Arguments>
    <GatheringServiceData>
      <Wizard xmlns="https://schemas.microsoft.com/pag/gax-wizards" SchemaVersion="1.0">
        <Pages>
          <Page>
            <Title>Hello World</Title>
            <Fields>
              <Field ValueName="ClassName" Label="Class Name" />
              <Field ValueName="TargetNamespace" Label="Namespace" />
              <Field ValueName="HelloMessage" Label="Message" />
            </Fields>
          </Page>
        </Pages>
      </Wizard>
    </GatheringServiceData>
    <Actions>
      <Action Name="GenerateMainClass" Type="Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates.TextTemplateAction, Microsoft.Practices.RecipeFramework.VisualStudio.Library" Template="Text\Program.cs.t4">
        <Input Name="Hello" RecipeArgument="HelloMessage" />  
        <Output Name="Content" />
      </Action>
      <Action Name="AddMainClass" Type="Microsoft.Practices.RecipeFramework.Library.Actions.AddItemFromStringAction, Microsoft.Practices.RecipeFramework.Library" Open="true">
        <!--Shows how to specify a property as a fixed value in config-->
        <Input Name="Content" ActionOutput="GenerateMainClass.Content" />
        <Input Name="TargetFileName" RecipeArgument="TargetFile" />
        <Input Name="Project" RecipeArgument="CurrentProject" />
      </Action>
    </Actions>
</Recipe>

See also

Specifying Command Bars | Specifying Bound References | Specifying Unbound References | Gathering Arguments | Using XInclude in the Guidance Package Configuration File