Thursday, September 9, 2010

Vs2010 Code Analysis Custom Rules

I've written my first custom rules for visual studio to help me on my team with the written and unwritten rules. It was a long journey but I have the hang of it. These rules are set to run on every debug build I do and take about 8 seconds to run all.

 As I understand it custom rules ( Inherited from Microsoft.FxCop.Sdk) can also be used as code check in policies on TFS or in the build process to produce a nice xml report.

Here's the base class (.net 4.0 class library project type)


using Microsoft.FxCop.Sdk;
 
public abstract class BaseRule:BaseIntrospectionRule
{
 protected BaseRule(string ruleName)
  : base(
   ruleName,
   //typeof(BaseRule).Assembly.GetName().Name+".Rules"
   "ProjectNamespace.RuleMetadata",
   typeof(BaseRule).Assembly
   ) { }
}


This class was defined without a namespace, I believe it did not work while it was in one. It's also quite important to note that the string literal Must match your project namespace+xml filename without the .xml on it. The commented method may work as well if your xml file is called Rules.xml. This file must be set to Embedded Resource.

Rules can be set as Messages, Warnings, or Errors and show up in the standard Error List control if code analysis is turned on in your project.

The general classes of rules I have written so far include:

  • Do not use method - Things like GC.AddMemoryPressure, MessageBox.Show (we use a custom dialog with styling)
  • Do not inherit - We don't want anyone inheriting from Windows.Forms.Form
  • Do not raise- We don't want code that has NotImplementedExceptions still present.
  • UseHungarian for controls
    • uses a dictionary to determine if the control has a preferred name we use on the team.
  • Fields should be private
  • Control Property must be set to x
    • For example, in most cases we want all dialog border styles to be fixed - I check the Initialize Component method of anything that inherits from Form for the property to be set there.
  • MustOverride - For legacy reasons (old Visual Studio bug they tell me) we have methods/properties that are virtual instead of abstract, and if they aren't overridden will throw a runtime exception.

Friday, August 20, 2010

Visual Studio find and replace with regular expressions totally sucks.

The syntax for regular expressions in Visual studio's find and replace option are incredibly convoluted.

I wanted to replace all instances of width in a style sheet with something that was variable based.

so width:75px; would become width:@(width=75)px;

This syntax is using the new Razor view engine and the result is we set a local variable and render it to the page in a nice terse expression. Combined with:

left:@(leftStart+width)px;

we now have the beginning of a nice sliced row of images. Where all the things on a particular row would incrementally build, and then on the next row, I can reset leftStart.

This was the syntax for finding all lines of an html document and capturing the width:
width\:{[0-9]+}

And the syntax for the replacement?
width\:\@(width=\1)

I understand that c# syntax uses a ton of the same conflicting symbols, but... could you guys make something like rexexpal so that we can iteratively solve for the expressions we need?

Wednesday, August 18, 2010

WPF DataGrid Row-level styling

It took me a very long time to figure out how to do this. The following code takes a WPF (and probably silverlight) datagrid and changes the row foreground color based on a single property in that row.

<DataGrid AutoGenerateColumns="True"  Name="dgProjects" >
    <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=HasProblems}" Value="True">
                            <Setter Property="Foreground" Value="Red"/>
                        DataTrigger>
                    Style.Triggers>
                   
                Style>
            DataGrid.RowStyle>
DataGrid>

Monday, August 16, 2010

Vs Add-in solution explorer context menus with a little MEF

My first MEF success. I wrote a visual studio add-in context menu for solution explorer where you can right click a project (or solution file for all projects) so that it:



  • reads the project file checking several problem areas 
    • hint paths
    • copy local
    • pre/post build events
    • target framework version
  • then makes a backup copy
  • cleans those local customizations
  • invokes msbuild to make sure it still builds
  • brings up the Source control commit dialog
  • restores your local customizations that we don't want in the source.
I used MEF so that the UI was not hard coded into the add-in. It  was amazingly simple. While I was developing a solution someone was nice enough to post on stackoverflow telling me you can't hydrate static properties. Which I did.

Thursday, July 15, 2010

Bookmarklet: Change the page title

So on some sites, when you get a new ajax message (think facebook, gmail, or maybe meebo) the new message indicator never goes away in the page title on your tabs. Also some titles aren't nearly as clear on your tabs as to what they are, so you can use this to set the title on your tabs to the domain of the page.


So this is what I've come up with as a nice bookmarklet to clear the title without having to reload the page.

javascript:void(function(){document.title=document.domain;}())

or if you like you can set it to document.location