Writing Object Use to Standard Output
A preponderance of fundamental SharePoint proxy objects implement the IDisposable pattern, and therefore the disposal state of such objects becomes a subject of interest, this is predominantly important when using mock object schemes. Recently, I had to do just this and it's not actually very complicated, but I needed to remember it so decided to post it. While the goal was to make the method analogous to type, the approach can be easily schemed to SharePoint proxy objects, since as you can see itβs a very simple approach.
In the below, the new static extension method TraceUsing in class Extensions is being defined. Since we are going to follow a object, the instance represents type "T" and the Action parameter set to type "T" lets you specify an action to be performed on "T". In my specific case, the parameter supply to Action was just a write to standard output saying that we are currently using an arbitrary object (to an HtmlTextWriter in my case).
-
public static class Extensions
-
{
-
-
public static void TraceUsing<T>(this T instance, Action<T> action)
-
{
-
try
-
{
-
action(instance);
-
}
-
finally
-
{
-
var disp = instance as IDisposable;
-
if (disp != null)
-
{
-
disp.Dispose();
-
}
-
}
-
}
-
}
1 Comment »
RSS feed for comments on this post. TrackBack URL























Articles & Research
SharePoint Architecture
Personal/Off-Topic
Article Or Research Filed Under 

[...] Writing Object Use to Standard Output [...]
Pingback by Links (1/29/2009 « Steve Pietrek - Everything SharePoint) — January 29, 2009 @ 7:45 pm