Why Overuse of Reflection can be a mistake …

 
I was working on a bug today on a customised List Viewer which I’d inherited.  The guy who wrote it was plainly a very clever chap and knew his SharePoint internals unfortunately even against his own advice on his blog (which I won’t include here), he added in some key functionality which depended upon a call to an internal method inside the Microsoft.SharePoint.dll.
 
list.GetType().InvokeMember("SetViewRenderQueryParameters", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance, null,list,new object[] { context, currentUrl, web, listName, viewGuid,designerView, gridview, instanceID, selectedID, filterString, rootFolder, folderCtId, paged, pagingTokens, lastFilterIndex });
 
Unfortunately, what he said could happen, did happen.  That is Microsoft updated Microsoft.SharePoint.dll and changed the signature of this method, and added in a new argument prevPaged! Therefore the InvokeMember failed and not surprisingly, so did the custom list viewer. 
 
The fix was easy enough thanks to Red Gate .NET Reflector, determining the correct signature was no problem, and the code was suitably updated, but really it needs a better way to do what has been done, as there is no guarantee that the same won’t happen again!
 
So the lesson is, stay away from using private members internal to DLLs to which you do not have the source code, the author is under no obligation to honour any contract with the outside world, that’s why the method is private …. the clue is in the name …
 
Cheers
 
Dave Mc
 
 

Advertisements

About davemcmahon81
Software Developer & Architect, User Group Leader, Speaker, Writer, Blogger, Occasional Guitarist, Man-made Global Warming Sceptic, Climate Change Believer, General Optimist but most of all proud Husband and Dad ...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: