#Google Analytic Tracker

Pages

Dec 12, 2008

C# Casting using Extension Method and VS2008 Debugger Issue

 

Since the creation of extension method in C# 3.0, my college and I have been happily writing extension methods which make our code look a lot nicer.

Then, I was surprised by my college when he took leverage of this by create an extension method for casting. Why not using the normal C# casting syntax?

   1:  public static TTargetType To<TTargetType>(this object oObject)
   2:  {
   3:      return (TTargetType)oObject;
   4:  }

Initially, I didn't like this idea too much, because it does have disadvantages when using a method to do casting:

The advantage of this method is that it makes typecasts way cleaner. The disadvantages are that you'll have to go one level down the stack trace to debug properly, and some erroneous casts might not get caught by the compiler.

However, after using the extension method for cast a couple of times, I really like use it. It does make the code a lot cleaner to read. In fact, whenever I notice my assignment is incorrect as I type, I can immediately hit the dot key to casting instead of moving my cursor back to the being of variable and type out the C# cast syntax:

   1:  private bool ApplyUnsplitWorkWorker(object[] oArgs)
   2:  {
   3:        sch_WorkData oUnsplitWork = oArgs[0].To<sch_WorkData>();
   4:        sch_WorkItemData[] aUnsplitWorkItems = oArgs[1].To<sch_WorkItemData[]>();
   5:        sch_WorkItemData oDeletedWorkItem = oArgs[2].To<sch_WorkItemData>();
   6:        sch_WorkData oDeletedWork = oArgs[3].To<sch_WorkData>();
   7:   
   8:  //...
   9:  }

Check out the bottom two examples , see which one you like more:

private string SomeMethod(DataRow oRow)
{
    //Extension cast
    return oRow.To<EmployeeRow>().Department.To<MathDepartment>().MathRoomCount.ToString();
}

private string SomeMethod2(DataRow oRow)
{
    //C# Cast
    return ((MathDepartment)((EmployeeRow)oRow).Department)).MathRoomCount.ToString();
}

In addition, there is as a extension method for casting that return a null if object is null or default value (such as DateTime), and an extension method for "as" syntax.

Yesterday, I encountered an exception when I was running a project. When I try to debug the issue, to my surprise one of the objects can't be analysis by the debugger, neither can it be display in the Watch Windows or the Immediate Windows. For testing, I create an extra variable call "l_oWorkItemRow2" using .C# casting, so that I can compare with "l_oWorkItemRow1"

Before:

 

Once I continue hit next line in the debugger, a null reference exception occurs:

What's going on? Why am seeing the variable properties saying "threw an exception of type 'System.NullReferenceException'"...

In fact, occasionally the debugger failed to analysis the property value in certain stack. If I move the debugger back to one stack where I pass the value, that I could than see the value.

The question is why the debugger was able to analysis the regular casting, and not the extension method cast. Is it just a current limitation to the debugger, or it is actually a bug? For example, do you know that only certain cases you can do "Edit and Continue" in the debugger.

Anyhow, if I find out more about this strange behaviour, I will definitely write another post!

No comments: