Base singleton class with Generics

Got this useful code snippet from Erik, for creating singleton classes using generics.

public class BaseClassSingleton where T : new()
{
	#region Singleton

	private static T m_Instance;

	public static T Instance
	{
		get
		{
			if (m_Instance == null)
				m_Instance = new T();
			return m_Instance;
		}
	}

	#endregion
}

To use:

public class Log : BaseSingleton {}

Ideal for use in cases where your singleton class doesn’t need to inherit from anywhere else.

Tags: ,

Unrecognized tag prefix or device filter ‘asp’

Another annoying Visual Studio 2005 error:

Unrecognized tag prefix or device filter ‘asp’

Only seems to occur when using master pages, and the master page is specified in web.config (rather than through the MasterPageFile attribute on the Page directive).

Adding in the MasterPageFile setting into the page seems to get rid of the problem. Reminds me of the problems I had earlier with controls. I’m going to avoid using web.config for these kinds of things where possible as it seems to cause more problems than it solves!

Visual Studio 2005 & ASP.NET Control woes

I’ve got an ASP.NET project with some custom controls, which subclass some standard controls.  Eg. MessageLabel subclasses Label, and adds some additional formatting and css properties.

Compiled controls are in same web project, in the MyProject.Controls namespace, and referenced into the project through web.config by adding the following under system.web -> pages -> controls.

<add tagPrefix=”myproj” namespace=”MyProject.Controls” assembly=”MyProject” />

On ASPX pages, controls are added using the following syntax:

<myproj:MessageLabel ID=”MessageLabel1″ runat=”Server” />

Except, this breaks the designer file! As soon as the control is added to the page, adding any additional controls or saving the page will show the following error in the errors list:

Generation of designer file failed: Unknown server tag ‘myproj:MessageLabel’

This is a big annoyance, as it means that this and any other controls added to the page will not be updated in the [no-longer] generated designer file, stopping them from being accessed in the code-behind.

The only workaround I’ve found is to manually add the register directive at the top of each ASPX page:

<%@ Register Assembly=”MyProject” Namespace=”MyProject.Controls” TagPrefix=”myproj” %>

…which completely defeats the purpose of adding the assembly reference in web.config.  Some other people experiencing this problem a while back reported that it was fixed in VS2005 SP1, but I’m using SP1 and in my experience it doesn’t seem this is the case.

Very, very annoying.

Friendly display of enums

When binding enums to a dropdown, I find it useful to run the enum text through a regex to make it more user friendly to view.

Eg. ‘User Account Disabled’ in a dropdownlist looks better than ‘UserAccountDisabled’.

public static string SplitIntoSentence(string s)
{
	Regex re = new Regex("([a-z]{0,})([A-Z])([a-z]+)", RegexOptions.Singleline);
	string a = re.Replace(s, "$2$3 ");
	return (a.Trim());
}

Although mainly used for enums (as they don’t allow spaces), this method can be used on any string of text and will insert a space before every capital letter to break up a list of words.