Useful Utility functions in .Net a.k.a ‘Hidden Treasures’

It's no secret that Microsoft's is releasing the source for some of its core .Net classes in upcoming visual studio 2008. Before that arrives, the option to look at some of core classes is probably the .Net Reflector. Every now and then, when I want to find out how Microsoft implemented some of the functionalities in core classes, or if I want to learn better way to program, I use the reflector to dig through the core components. During these adventures, I have bumped into various classes and functions, that I thought were just too cool, and also unfortunate that most were internal only. One of my favorites is the VirtualPath class. However, during this quest I have also found that in some way or other the framework team did expose some great utility functions. Most developers aren't aware of this, including myself and make the mistake of rewriting it. Thus, here's a shot at exposing some of the cool Utility Classes and Functions (I have been using) that every .Net developer should know. (I hope to keep adding to this list, if anyone has other suggestions please feel free to add.)

 

ASP.NET

 

System.Web.HttpUtility
System.Web.HttpServerUtility

Provides helper methods for processing web requests like HTMLEncode, UrlEncode, HTMLDecode
Accessible through 'Server' object in asp.net

System.Web.VirtualPathUtility

Provides utility methods for common virtual path operations.
Members: GetDirectory(), GetExtension()

System.Web.Compilation.BuildManager
.CreateInstanceFromVirtualPath()

One of my favorites, allows you to create instance of pages/controls just by passing a virtual path.

System.Web.Security.UrlAuthorizationModule
.CheckUrlAccessForPrincipal()

Another great utility in asp.net that allows you to check permission on the file even before you use it. Usage? ex: loading sitemap based on the permission set on each file rather than specifying roles in the node.

System.Web.Hosting.HostingEnvironment

Various methods/properties related to hostingenvironment

System.Web.Hosting.HostingEnvironment
.VirtualPathProvider

Retrieves virtual path provider for the application

   

.NET

 

System.IO.Path

Provides helper methods for file/directory strings

Members: ChangeExtension(), GetFileName, Combine…

Int? DateTime?

These are nullable classes of primitive types.

Int.TryParse

Equivalent to IsNumeric() function in VB.

 

NOTE: There are tons of other classes & static functions in .NET, it's a best practice to use those. If you can't find one exposed but is internal/private, I would suggest creating one similar to it.

Print | posted on Wednesday, October 17, 2007 6:03 PM

Feedback

# re: Useful Utility functions in .Net a.k.a ‘Hidden Treasures’

Left by Dave at 10/18/2007 1:36 AM
Gravatar I thought these (HTMLEncode, UrlEncode, HTMLDecode) were in System.Web.HttpUtility

# re: Useful Utility functions in .Net a.k.a ‘Hidden Treasures’

Left by Bigyan at 10/18/2007 10:18 AM
Gravatar Dave, you are correct. It is exposed through HttpUtility class, but is also available for instantiated HttpServerUtility objects.

Your comment:





 
Please add 6 and 6 and type the answer here:

Copyright © Bigyan Rajbhandari