gists, net comments edit

I’m working on some custom FxCop rules and one that I want to do is to catch people who try to call Dispose() on objects deriving from System.ServiceModel.ClientBase<T> because they didn’t implement IDisposable in a safe manner.

So you have ClientBase<T> which looks, in a very abbreviated fashion, like this:

public abstract class ClientBase<TChannel> : ICommunicationObject,
          IDisposable where TChannel : class
{
  // Other stuff... and then
  void System.IDisposable.Dispose()
  {
    this.Close();
  }
}

Later, I might have a class that derives from that. Maybe a special type of client, and I might implement my own safe IDisposable version:

public class CustomClient : ClientBase<IMyService>,
          IDisposable where TChannel : class
{
  // Other stuff... and then
  void System.IDisposable.Dispose()
  {
    try
    {
      this.Close();
    }
    catch
    {
      this.Abort();
    }
  }
}

Try not to get hung up on the hokey implementation there, just stick with me - you have a sort of “overridden” Dispose() call. The thing is, if I put my CustomClient in a using statement, it’s the “overridden” Dispose() that executes, not the one in ClientBase<T>.

I want my FxCop rule to catch people who put something deriving from ClientBase<T> in a using block, but if you’ve got an override like in the CustomClient class there, I want it to let it go.

How do you detect that?

I’ve been all over the System.Reflection namespace and I can’t find anything. If you do Type.GetInterfaces() or Type.GetInterface() it shows that you implemented IDisposable either way because it gets all of the interfaces you implement all the way through the inheritance chain. Type.GetInterfaceMap() only returns the base implementation - the one from ClientBase<T> - in all cases. It ignores the derived class’s “override.” The only thing I can figure out that seems to work, but feels really bad, is this:

public static bool OverridesDispose(Type runtimeType)
{
  // For brevity, we're assuming the incoming Type isn't null and
  // implements IDisposable. I've omitted those checks here.
  MethodInfo info = runtimeType.GetMethod(
    "Dispose",
    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
    null,
    new Type[] { },
    null);
  if (info == null)
  {
    info = runtimeType.GetMethod(
      "System.IDisposable.Dispose",
      BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
      null,
      new Type[] { },
      null);
  }
  if (info == null)
  {
    return false;
  }

  Type declaringType = info.DeclaringType;
  if (
    declaringType.IsGenericType &&
    declaringType.GetGenericTypeDefinition() == typeof(ClientBase<>)
  )
  {
    return false;
  }
  return true;
}

See what I’m doing? I basically query for an implicit interface implementation, then if that’s not found, I get the explicit interface implementation. If neither are found, I figure there’s no override. If one is found, then I ask what the declaring type of the method is, and if it’s the ClientBase<T> type, it’s not overridden, otherwise it is.

But the code smell! Ugh!

Am I missing some easier way to do it?

Last week was #32 for me. I had a great time the weekend before last, checking out The Dark Knight and hitting the driving range.

On the day of my birthday my parents came over and brought dinner - Mom made this super-tasty Mexican meal. I ate far too much of that and had some cheesecake when it was all done. They also got to visit the new kittens, who are starting to get big but no less wild. Jenn got me a red and black Nintendo DS with Guitar Hero: On Tour. My parents got me a case for all that and Mario Kart DS.

This past weekend, we went to the Washington County Fair on both Friday night and Saturday. I rode one of the rides called “El Niño” with Jenn and our friends Angela and K. It was pretty crazy and probably one of the better traveling fair rides I’ve been on. We also saw The Pirate’s Parrot show and it was great as usual, even though I got a pretty decent sunburn out of it. I hope they come back again next year.

In the end, it was a pretty great birthday. It’s going to be a while before I reach my next power of 2, so I’m going to have to savor this one.

net, vs comments edit

Just got word from the folks over at Typemock

  • if you have an open source project and you want to use Typemock Isolator, they’re going to be coming out with a new (free) license type that allows your open source project to use it. Here’s the new licensing structure I’ve seen:

  • Commercial - $449 (same as today)
  • Personal - $200 (will include one year maintenance)
  • Open Source Project - Free
  • 21-Day Trial - Free

And, of course, discounts for upgrades, etc., on the paid licenses.

This is huge. I’ve been wanting to add tests to CR_Documentor, but it’s almost impossible to do that because of the tight coupling DXCore plugins have with the DXCore engine. For example, I’d love to be able to create a fake language element and unit test the syntax generator… but I don’t want to have to write wrapper interfaces and services for everything in DXCore that I interact with. Now I can!

Big side benefit - the folks who are unfamiliar with Isolator can see how open source projects use it and see the benefits. Sweet!

downloads, vs, coderush comments edit

I just posted the new version of CR_Documentor, version 2.1.0.0, over at Google Code. This new one hosts a tiny internal web server (instead of manually poking things into the IE DOM) so as long as you can get to localhost, you should be able to see the preview without that nasty warning. I also fixed the focus issue so it shouldn’t steal focus when the preview refreshes.

If you upgraded before and are ready for a fix, go get it!

web comments edit

Jeff Atwood posts about the exact thing I thought when I saw the MVC tidal wave coming to overtake web forms. It feels like I’m back in the 90’s, doing old-school ASP, because of the tag soup.

Don’t get me wrong - I’m all about separation of concerns and such, but every time I look at something like MVC, I see all roads leading to web forms. Maybe different web forms, but web forms nonetheless.

  • Too much tag soup? Package stuff up in helper methods.
  • Oh, wait, the helper methods that generate control structures aren’t really flexible enough to allow overridden behaviors or wire things up (like validation). Let’s make them instance methods on objects.
  • Hmmm, there’s sort of an object lifetime to manage here, and wouldn’t it be nice if the packaged widget could be a little smarter about handling view data? Like dynamically populating itself based on data and letting me know if the user changed stuff, because writing that every time is such a pain.
  • Hang on, that’s web server controls.

Some folks argue they want that “tighter control over the HTML” that the tag soup provides. I, personally, am much less interested in hacking HTML. Let the framework do it for me. I’m more concerned with the business logic anyway.

I’ve done my share of classic ASP style development in lots of different server side languages, and it always ends up that to get rid of the tag soup, you head towards web forms.