Feed on
Posts
Comments

One of my favorite namespaces is ‘Sitecore’.  It contains so many Util classes I commonly use:

DateUtil – Providing parse and serialize functionality for DateTime’s, TimeSpan’s and ISO-Dates.
Converting/Parsing
DateTime – ParseDateTime(String value, DateTime default)
DateTime – ParseDateTime(String value, DateTime default, CultureInfo ci)
TimeSpan – ParseTimeSpan(String value)
TimeSpan – ParseTimeSpan(String value, TimeSpan default)

MainUtil – Environment Util from reflection to IO and from Drawing to Diagnostics.
Some collection utils:
BuildHashtable -> Easily build an Hashtable by a set of object(params or array)
AddArrays -> Merging Arrays incl explicit casting
CompactArray -> Clean up an array when it contains some nulls/empties, ugly but very necesarry in case of untrusted input!
ConvertArray -> ArrayCaster. Might be deprecated as System.Array is extended a lot in 2.0.
GetExtraEntries -> Compare 2 Hashtables return difference… As a Hashtable ;)

Environment method:
GetProcesID, GetProcesInfo, GetProcesMemoryInfo, GetProcesName  -> Way to get to know how the current proces acts.
Trace -> Write a message to the Sitecore-trace
TraceIf -> Equal to Trace except the first argument it makes it act like Debug.Assert.
GarbageCollect -> …..

Reflection stuff:
CreateObject -> Quick invokation:

object MyObject = MainUtil.CreateObject(“/bin/MyAssembly.dll”, “MyNamespace.MyObject”);
object MyObject2 = MainUtil.CreateObject(“MyAssembly”, “MyNamespace.MyObject2″, parameter1, parameter2);

Others:
GetImage -> With all these overloads, any imagetag can be created.
FormatSize -> Convert a logn to a nice string such as ’2Mb’
AppendMissingIDBrase -> As described in the .chm: Appends a mssing closing brace “}” if a path starts with a brace “{“.
UmapPath -> Instead  of mapping, we’re going to unmap it today. Back to the relative paths!

StringUtil - All kind of System.String methods you wish you had access to before.
Most used:
GetString -> Overloaded method where you can define a value ‘to parse’ and a default one in case of null or empty. The overload provides you params, which selects the first var which isn’t null or empty(GetString(“”, null, emptyInputObject, “default!!”) -> “default!!”).
Unquote -> Remove single and doubles quotes in an efficient way:

string s0 = StringUtil.Unquote(“‘Hello world’”); // “Hello world”
string s1 = StringUtil.Unquote(“‘Hello Andy’s world’”); // “Hello Andy’s world”

CutUp -> Splitting string after x chars returning an ArrayList
Repeat -> Repeat a String n2-times.

UIUtil – Very quick way to access (browser-)variables and other usefull others.
Non XAML developers:
IsIE -> Indicates wether it’s IE or not.
ParseStyle -> Parse a StyleString to a string

There are lots of more methods and other Utils. Writing and explaining them all will take way to much time. Take a look at the Reference section on SDN. Make sure you look in the right API-documentation for the Sitecore version you are using.
By the way, there are some other Util-classes, not located in the Sitecore-namespace, but expand Sitecore.IO for example. Tomorrow, I’ll discuss the *IDs-classes in the Sitecore namespace. They are important aswell.

Anyway, next time, before writing your own Util-method, take a look in the above Util-classes, it might accelarate your development a bit! And for those who want to use them in as XSLT extention, take a look at this entry.
Goodnight :)

Leave a Reply