INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Managing Projects & Solutions

C# Singleton template for VS 2003 by chiph
Posted: 16 Jul 06

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 C:\Program Files\Microsoft Visual Studio .NET 2003\VC# 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.
  1. The relative path to the .vsz file
  2. {FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}
  3. C# Singleton Class
  4. 0
  5. A Singleton Class
  6. {FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}
  7. 4534
  8. 0
  9. Singleton.cs
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.

Back to C# (C sharp): Microsoft FAQ Index
Back to C# (C sharp): Microsoft Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close