Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Singleton template for VS 2003

Managing Projects & Solutions

C# Singleton template for VS 2003

by  chiph  Posted    (Edited  )
I found myself creating a large number of Singleton objects recently, and after cutting/pasting (and making the usual cut & paste coding mistakes) I decided to create a Visual Studio template for the process.

In your C# folder (located at [tt]C:\Program Files\Microsoft Visual Studio .NET 2003\VC#[/tt] on my machine), go into the CSharpProjectItems folder and create a new file named CSharpAddSingleton.vsz In this file, put this code:
Code:
VSWIZARD 7.0
Wizard=VsWizard.VsWizardEngine.7.1
Param="WIZARD_NAME = CSharpAddSingleton"
Param="WIZARD_UI = FALSE"
Param="PROJECT_TYPE = CSPROJ"
Go back to your VC# folder and create a new folder named CSharpAddSingleton. Create two new folders in there, named Scripts and Templates.

Under the Scripts folder, create a folder named 1033, create a file named default.js. In this file, put this code:
Code:
// CSharp add-in wizard for creating singletons
function OnFinish(selProj, selObj)
{
    var oldSuppressUIValue = true;
	try
	{
		var strTarget = wizard.FindSymbol("ITEM_NAME");
		var strClassName = strTarget.split(".");
		var bValid = wizard.ValidateCLRIdentifier(strClassName[0]);
		if (!bValid)
		{
			wizard.ReportError();
			return VS_E_WIZARDBACKBUTTONPRESS;
		}
        oldSuppressUIValue = dte.SuppressUI;
		var strProjectName = wizard.FindSymbol("PROJECT_NAME");
		var strSafeProjectName = CreateSafeName(strProjectName);
		wizard.AddSymbol("SAFE_PROJECT_NAME", strSafeProjectName);
		SetTargetFullPath(selObj);
		var strProjectPath = wizard.FindSymbol("TARGET_FULLPATH");
		var strTemplatePath = wizard.FindSymbol("TEMPLATES_PATH");

		var strTpl = "";
		var strName = "";
		var InfFile = CreateInfFile();

		AddReferencesForClass(selProj);
		AddFilesToCSharpProject(selObj, strProjectName, strProjectPath, InfFile, true);
	}
	catch(e)
	{
		if( e.description.length > 0 )
			SetErrorInfo(e);
		return e.number;
	}
    finally
    {
   		dte.SuppressUI = oldSuppressUIValue;
   		if( InfFile )
			InfFile.Delete();
    }
}

function SetFileProperties(oFileItem, strFileName)
{
}
This javascript code gets called by the IDE to customize the template that we're creating next.

Go to the Templates folder we created earlier, and create a 1033 folder, and in that folder, we'll create two files.

First one is NewCSSingleton.cs, which will be the code you get out of the wizard, containing several replacement tags:
Code:
using System;

namespace [!output SAFE_NAMESPACE_NAME]
{
	/// <summary>
	/// Summary description for [!output SAFE_CLASS_NAME].
	/// </summary>
	public sealed class [!output SAFE_CLASS_NAME]
	{
		static readonly [!output SAFE_CLASS_NAME] _instance = new [!output SAFE_CLASS_NAME]();

		// Explicit static constructor to tell C# compiler
		// not to mark type as beforefieldinit
		static [!output SAFE_CLASS_NAME]()
		{
		}

		[!output SAFE_CLASS_NAME]()
		{
		}

		public static [!output SAFE_CLASS_NAME] Instance
		{
			get
			{
				return _instance;
			}
		}
	}
}
The next one is Templates.inf, which tells Visual Studio the name of the template file. It contains:
Code:
NewCSSingleton.cs
Now for the complicated part.
Go to the VC# folder, then follow down the CSharpProjectItems folder and the LocalProjectItems folder. Create a new file in this directory called CSharpAddSingleton.vsdir, which is what Visual Studio uses to display and locate the other files.

In the CSharpAddSingleton.vsdir file, we'll create several items which are separated with vertical pipe (|) characters.
[ol][li]The relative path to the .vsz file[/li]
[li]{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}[/li]
[li]C# Singleton Class[/li]
[li]0[/li]
[li]A Singleton Class[/li]
[li]{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}[/li]
[li]4534[/li]
[li]0[/li]
[li]Singleton.cs[/li][/ol]

This should look like the following (all on one line, in case your browser wraps this text):
Code:
..\CSharpAddSingleton.vsz|{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}|C# Singleton Class|0|A Singleton class|{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}|4534|0|Singleton.cs
And that should do it. Now when you open Visual Studio 2003, and create a new project, and select "Add new item..." from the File menu, you should see your new singleton template in the list. Select it, and it will create a new file called Singleton1.cs in your project.

Read more at:
http://msdn2.microsoft.com/en-us/library/2sc7ft4a.aspx

Chip H.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top