Reflection is case sensitive by default, which comes at no surprise to most C# developers.However, it is easy to forget the case sensitivity issue sometimes especially when doing web development. I recently ran into such an issue and it took me a while to realize that the problem I had actually had something to do with the case sensitivity issue while using reflection.

I was doing some ASP.NET work and was using customized objects as data sources to bind to DataGrid. For sorting, I used the technique similar to what described in Sorting GridView Using IComparer. When I was testing my grids, however, I noticed that for most of the columns it worked perfectly, but for a few I somehow was greeted with the null reference error.

At first I thought that this was caused by the null items in those columns. After spending quite sometime digging, I realized that this was due to the case sensitivity issue in reflection. I had used the wrong case in the DataField of  the BoundField. And after I changed the spelling to match that defined in the value object, the problem was solved.

VB developers should pay special attention when using reflections, as in VB.Net the language itself is not case sensitive.

As a side note, even though it is possible to set the binding flag to make the member lookup case insensitive, it is not recommended as such approach tend to make the operation less efficient.


Be Sociable, Share!