Visual Studio 2008 Crashes When Applying Custom Attribute – BUG and Solution

I’ve been moving my business layer projects from Visual Studio 2005 to Visual Studio 2008 and ran into a very nasty bug that crashes Visual Studio 2008 when applying a custom attribute.  I have reported this bug to Microsoft here.  Others on the Internet had similar problems, so I thought I would post my work around until the Visual Studio Team can provide a hot fix.

Bug

When applying a custom attribute to a property, and the custom attribute has a default empty constructor and other overloaded constructors, Visual Studio will crash every time when you type the “(”.  It seems this is an Intella-Sense issue.  Because if I edit the problem attribute in Notepad and then recompile the program runs fine.  I just can’t edit it using Visual Studio.

I have not confirmed this bug on C#, only on VB.NET.  I can reproduce it every time I try it in VB.NET.

This bug is not in VS2005, just VS2008.

Workaround

Unless you need a default empty constructor, just remove it and everything works fine.

In the video supplied with this post, I demonstrate other workarounds too.  There are several.

Download Source Test Project & Video

After downloading this file, you must change the file extension to .zip, this is a requirement of WordPress.com.

Visual Studio 2008 Apply Custom Attribute Bug Download

NOTE: I have included a short Silverlight Streaming video available at the bottom of this post to demonstrate the problem and workarounds.

Code To Reproduce

Simple Customer class with a FirstName property that we want to apply a custom attribue to.

Public Class Customer 

  Private _strFirstName As String = String.Empty 

  Public Property FirstName() As String
    Get
      Return _strFirstName
    End Get
    Set(ByVal value As String)
      _strFirstName = value
    End Set
  End Property 

End Class

The below custom attribute has the extra empty constructor.  From the code, it is obvious that if this attribute is applied with the empty constructor, that the minimum length is 0.   Not very useful in code, just trying to illustrate the bug.  If you attempt to apply this attribute to the above FirstName property and press the “(” to enter a value for the second constructor, VS crashes!

Public Class StringLengthValidationAttribute
  Inherits Attribute 

  Private _intMinimumLength As Integer = 0 

  Property MinimumLength() As Integer
    Get
      Return _intMinimumLength
    End Get
    Set(ByVal value As Integer)
      _intMinimumLength = value
    End Set
  End Property 

  Public Sub New() 

  End Sub 

  Public Sub New(ByVal intMinimumLength As Integer)
    _intMinimumLength = intMinimumLength
  End Sub 

End Class

Good Code (Work around code)

You can see that the default constructor has been removed and the attribute can now be applied to the FirstName property.

Public Class StringLengthValidationAttribute
  Inherits Attribute 

  Private _intMinimumLength As Integer = 0 

  Property MinimumLength() As Integer
    Get
      Return _intMinimumLength
    End Get
    Set(ByVal value As Integer)
      _intMinimumLength = value
    End Set
  End Property 

  Public Sub New(ByVal intMinimumLength As Integer)
    _intMinimumLength = intMinimumLength
  End Sub 

End Class

Below is the Customer class with the custom attribute applied to the FirstName property.

Public Class Customer 

  Private _strFirstName As String = String.Empty 

  <StringLengthValidation(1)> _
  Public Property FirstName() As String
    Get
      Return _strFirstName
    End Get
    Set(ByVal value As String)
      _strFirstName = value
    End Set
  End Property 

End Class

Video Walk Through and Solution

The video links require Microsoft Silverlight 1.0. If you do not have it, you will be prompted to install it when you click one of the links. After the short installation is completed, close the browser window that you did the install in and re-click the video you want to watch. You can also download it here. Windows XP or Vista required.

Visual Studio 2008 Applying Custom Attribute Bug – Solutions Video

Silverlight Video Player

Video Player

When you click on one of the above links your browser will open a new window and display the video player. You can resize your browser window and the video player will resize with the browser. You can also click on the Full Screen button on the video player controller. Additionally, single clicking the video will pause it. Double clicking the video is another way to switch to Full Screen mode.

Hope this post can help someone get around this problem.

Just a grain of sand on the worlds beaches.

Leave a Reply

You must be logged in to post a comment.