Posts
203
Comments
1117
Trackbacks
51
January 2006 Entries
FxCop IDE Integration with VS2005 Professional Edition

VS2005 Team System has the capability of seamlessly integrating FxCop inside the IDE.  This is also possible with non-Team System versions such as Professional edition by using External Tools - the FxCop documentation for this can be found here:

http://www.gotdotnet.com/team/fxcop/Docs/FxCopCmd/FxCopCmd_VS.html

So what happens if you want to have this convenient IDE integration but point your analysis at an *.FxCop project file rather than pointing directly to assemblies?  The main thing you have to do is tell the tool (via the command line) where to look.  For example, the easiest solution would be to decide on a “standard” such as always storing your *.fxcop file in the solution root and naming it consistently (e.g., SolutionAnalysis.fxcop).  If you do that, then all you have to do to get FxCop to analyze with IDE integration is 1) follow the standard help link from above, and 2) set these command line values:

Title: : &FxCop
Command:  C:\Program Files\Microsoft FxCop 1.35\FxCopCmd.exe
Arguments:  /console /p:SolutionAnalysis.FxCop
Initial Directory:  $(SolutionDir)

Once you do that, all your analysis will be displayed in the Output console window within the IDE.  What's even nicer is that when FxCop flags you for errors, you can double-click on the message in the console window and it will automatically take you to the offending line of code.

Posted On Tuesday, January 31, 2006 1:18 PM | Comments (2)
Nullable Data Readers

I recently submitted an article about Nullable Data Readers .NET 2.0 for review which was posted here:

http://www.codeproject.com/csharp/NullableReaders.asp

As a quick supplemental example for that article, suppose you have a non-nullable type in your business object that you're populating with from a nullable field in the database (this might also happen when populated from an outer join).  This is a good opportunity to use the new nullable coalesce operator (aka, conditional assignmenet) in C# 2.0 like this:

someObject.IsActive = dr.GetNullableBoolean(”IsActive”) ?? false;

Posted On Friday, January 20, 2006 5:28 PM | Comments (0)
TryParse for Nullable Types

In my last post, I discussed creating a static class for Parsing nullable types:

http://geekswithblogs.net/michelotti/archive/2006/01/16/66014.aspx

However, 2.0 also introducing a new TryParse() pattern so that developers would not have to rely on catching exceptions when attempting a Parse() method.  For example:

http://msdn2.microsoft.com/en-us/library/ch92fbc1.aspx

We can incorporate the TryParse pattern into our NullableParser class as well so that our consuming code to look something like this:

DateTime? defaultDate = DateTime.Now;

NullableParser.TryParseNullableDateTime(s, out defaultDate);

person.DateOfBirth = defaultDate;

 

Internally, we can implement this the same way as the ParseXXX() methods by leveraging delegate inference and generics.  First define the delegate:

   1:  private delegate bool TryParseDelegate<T>(string s, out T result);

Now define the private generic method:

 

private static bool TryParseNullable<T>(string s, out Nullable<T> result, TryParseDelegate<T> tryParse) where T : struct

{

  if (string.IsNullOrEmpty(s))

  {

    result = null;

    return true;

  }

  else

  {

    T temp;

    bool success = tryParse(s, out temp);

    result = temp;

    return success;

  }

}

 

Now each public method is trivial to implement:

 

public static bool TryParseNullableDateTime(string s, out DateTime? result)

{

  return TryParseNullable<DateTime>(s, out result, DateTime.TryParse);

}

private delegate bool TryParseDelegate<T>(string s, out T result);

Posted On Monday, January 16, 2006 6:35 AM | Comments (11)
Parse Nullable Types

Often when working with a textbox or some other control, it is necessary to call the Parse() method to do a conversion for our business object property.  For example, consider a web form where you are assigning:

person.DateOfBirth = DateTime.Parse(txtDateOfBirth.Text);

This is all well and good if your field is required and you've already got a UI validation ensuring that the user typed a date into the text box.  But what do you do if the field is not required and you'd like to use Nullable for your DateOfBirth property.  Rather than littering your code with a bunch of if/then statements or creating methods to do it, the best bet is to create a re-usable class that can do this for you so that you're syntax can look like this:

person.DateOfBirth = NullableParser.ParseNullableDateTime(txtDateOfBirth.Text);

NullableParser is a static class (a new feature of C# 2.0) that has a ParseXXX() method for each value type.  To implement this internally, our method could simply look like this:

public static DateTime? ParseNullableDateTime(string s)

{

  if (string.IsNullOrEmpty(s))

  {

    return null;

  }

  else

  {

    return DateTime.Parse(s);

  }

}

 

The problem with this is that I have to write a repetitive IF statement for every single method in my static class - the only thing that would be different would be the return DateTime.Parse(s);  line.  In order to solve this issue by making the code more concise, we can leverage a delegate with delegate inference (another new C# 2.0 language features) within a generic method (generic method being another C# 2.0 new language feature).  First, we might define our delegate:

private delegate T ParseDelegate(string s);

Next we define the generic method passing the appropriate delegate:

private static Nullable<T> ParseNullable<T>(string s, ParseDelegate<T> parse) where T : struct

{

  if (string.IsNullOrEmpty(s))

  {

    return null;

  }

  else

  {

    return parse(s);

  }

}

With this simple generic method defined, the rest of the class is trivial as now all of the ParseXXX() methods can be rewritten like this (utilizing the appropriate Parse() method for delegate inference):

public static int? ParseNullableInt(string s)

{

  return ParseNullable<int>(s, int.Parse);

}

Leveraging the new features of C# 2.0 (i.e., nullable types, delegate inference, and generics) can result in code that is more efficient, concise, and easier to maintain.

Posted On Monday, January 16, 2006 6:20 AM | Comments (14)
SuppressMessage with FxCop and NON-Team System Visual Studio

If you're using Visual Studio 2005 Team System then FxCop is completely integrated and when you exclude messages it automatically applies the new SuppressMessage attributes to the appropriate location in code. But if you're using Visual Studio Professional edition then you have to use the external FxCop 1.35 UI. If this is the case, you can STILL utilize the SuppressMessage attribute but have to do a couple of things first. I had to do a little digging but the steps are pretty simple.

It turns out you have to enable your project to recognize the SuppressMessage attribute by FIRST adding a condition compilation symbol.

To include this symbol do the following:
C#
1. In Solution Explorer, right-click your project and choose Properties
2. In the Properties window, choose the Build tab
3. In the Conditional compilation symbols text box enter CODE_ANALYSIS

VB
1. In Solution Explorer, right-click your project and choose Properties
2. In the Properties window, choose the Compile tab and click Advanced Compile Options
3. In the Custom constants text box enter CODE_ANALYSIS
Found these steps here:

Now for the cool part. In Team System, you can just right-click on the rule and say “Suppress” and it will automatically add the SuppressMessage attribute in the right spot. In Professional edition, we don’t have this integrated into the IDE so we have to use the normal FxCop 1.35 UI (which of course has no knowledge of Visual Studio) – and correctly specifying these attributes is not easy to do by hand. It turns out all you have to do, is to right-click the error message in FxCop UI and select “Copy As” – “SuppressMessage” (or “Module-level SupressMessage”). Then you can simply paste the attribute into your code at the appropriate location. Very slick!

Posted On Wednesday, January 11, 2006 1:40 PM | Comments (4)

View Steve Michelotti's profile on LinkedIn

profile for Steve Michelotti at Stack Overflow, Q&A for professional and enthusiast programmers




Google My Blog

Tag Cloud