Monday, December 29, 2008

Add file header

To add C# file header according to StyleCop's SA1633 you can use this macro:

Sub AddFileHeader()


        DTE.ActiveDocument.Selection.Text = "//-----------------------------------------------------------------------"


        DTE.ActiveDocument.Selection.Text = "// <copyright file=""" + DTE.ActiveDocument.Name() + """ company=""MyCompany d.o.o."">"


        DTE.ActiveDocument.Selection.Text = "//    Copyright (c) MyCompany d.o.o. All rights reserved."


        DTE.ActiveDocument.Selection.Text = "// </copyright>"


        DTE.ActiveDocument.Selection.Text = "//-----------------------------------------------------------------------"


    End Sub

Friday, December 26, 2008

Visual Studio - Create Blank Solution

If you want first to create blank solution in Visual Studio on specific location and then to add existing projects or create new one you will be very disappoint if you expect to find "Create Blank Solution" item under the New menu.
To create blank solution do the following:
  1. On the File menu, select New and then click Project.

  2. In the Project types pane, select Other Project Types and then select Visual Studio Solutions.

  3. In the Templates pane, select Blank Solution.

Thursday, December 18, 2008

Visual studio test environment and provide additional files

If you want, for example, to copy additional files to test location for some unit test, for example, some XML file, do the following:
  1. Add test.xml to test project
  2. Set "Copy to output directory" property of file to "Copy always"
  3. For test method add [DeploymentItem("test.xml")] attribute.

Get current exe assembly folder location

string folderPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

Thursday, December 11, 2008

ASP.NET IIS trouble

If you, by mistake, first installed .net framework, and after that IIS you may have trouble to run your IIS/Web service applications.
One of the possible error messages can be: "Failed to access IIS metabase", or "service unavailable"...
To repair ASP.NET you should run:

aspnet_regiis -u
aspnet_regiis -i

It worked for me :)

Monday, December 08, 2008

This will save you some time

If you get error message "Association references unmapped class" from nhibernate check settings for Build Action for hbm.xml file. It should be "Embedded Resource".

Wednesday, December 03, 2008

Get calling method name

To get method name that was called current method use:

System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(1);
System.Reflection.MethodBase methodBase = stackFrame.GetMethod();
string methodName = methodBase.Name;
Console.WriteLine("Method name: "+ methodName);

Tuesday, December 02, 2008

ctor snippet upgrade

According to the StyleCop you should put this comment above constructor.

/// Initializes a new instance of the MyClass class.

In order to do this automatically you can use SnippetDesigner (or you can do it by yourself) to change default ctor snippet.
It should be:

/// Initializes a new instance of the $classname$ class.

public $classname$ ()

Snippet location is:
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#\ctor.snippet