This picks up with the code generation series. See other posts:
In this post we will be using project templates for code generation. Project templates are great when you are going to create many projects with similar boilerplate type code. For example if there is a plugin architecture and each plugin has one (or many) interfaces that it must implement.
In order to get started create a new project inside a solution. Write all the boilerplate code that is needed. Use the Template Parameters in the boilerplate code to make replacements. Note: it’s okay if the project doesn’t compile after adding template parameters. Make sure to setup any project specific build events, assembly references, output paths, etc. inside the project.
/*************************
* File: $safeprojectname$Plugin.cs
* Author: $username$
* Created: $time$
************************/
using Main;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace My.Plugins.Plugin.$safeprojectname$
{
///
///
public class $safeprojectname$Plugin : IPluginInterface
{
///
///
void IPluginInterface.DoWork()
{
// TODO: This MUST be implemented
throw new NotImplementedException();
}
///
///
///
string IPluginInterface.GetDefaultInfo()
{
return "Plugin ($safeprojectname$) created by $username$";
}
///
///
///
bool IPluginInterface.ReadyToGo()
{
return false;
}
}
}
After the boilerplate code is in go to File > Export Template…
This will bring up the “Export Template Wizard”. Make sure to select the project with the boilerplate code and click next.
The next page of the wizard allows some customization of the project name. Make sure to automatically import into visual studio.
That it. Write code once, reuse multiple times. To test out the new project template go to New > Project in Visual Studio. Since the project was automatically imported the new project template is available.
The new project will have the file(s) specified with all the template parameters filled in:
/*************************
* File: My_Plugin_Template1Plugin.cs
* Author: Sirchris
* Created: 3/28/2013 6:52:06 AM
************************/
using Main;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace My.Plugins.Plugin.My_Plugin_Template1
{
///
///
public class My_Plugin_Template1Plugin : IPluginInterface
{
///
///
void IPluginInterface.DoWork()
{
// TODO: This MUST be implemented
throw new NotImplementedException();
}
///
///
///
string IPluginInterface.GetDefaultInfo()
{
return "Plugin (My_Plugin_Template1) created by Sirchris";
}
///
///
///
bool IPluginInterface.ReadyToGo()
{
return false;
}
}
}
That’s a quick bear minimum of what can be done with project templates. Project templates can package up any set of files. In the exported template created look at the vstemplate file. There you can start to get a feel for some of the additional items that could be accomplished with templates. Make sure to look at the VSTemplate section on the MSDN.
Here is the vstemplate file created from this simple example
This is one of the simplest (and powerful) ways to get started with code generation. Keep them mind next time creating multiple project that are similar in nature. There are still two more examples of code generation coming up.