I'm beginning to appreciate the power of extension methods. At first I thought: "Who wants this?". Doesn't this 'break' the encapsulation of object oriented programming?
Well yes it does, but it makes for some darn pretty code when building utility methods. Right now I'm working on some stuff with the DataContractJsonSerializer and this class has a ReadObject method which takes a stream as a parameter. When writing unit test (and regular code) I'm quite frequently going from a string to a stream. So I figured I'd create an extension method called ToMemoryStream(). This is what is looks like:
using System;using System.IO;
namespace DevelopOne.Framework.Serialization{ /// <summary> /// Class with extension methods which are helpful when working /// with serialization. /// </summary> public static class Extensions { /// <summary> /// Converts a string to a memory stream and sets the position of the stream to /// the beginning of the stream. /// </summary> /// <param name="s"></param> /// <returns></returns> public static MemoryStream ToMemoryStream(this string s) { MemoryStream ms = new MemoryStream(); byte[] bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(s); ms.Write(bytes, 0, bytes.Length); ms.Position = 0; return ms; } }}
Now calling this method can be done by simply including the namespace DevelopOne.Framework.Serialization and calling ToMemoryStream() on a string.
Like this:
string json = "{\"exceptions\":[{\"type\":\"ExceptionObject\",\"errorMessage\":\"Calls to member.login must be secure.\",\"errorInfo\":null,\"errorCode\":513}]}";MemoryStream ms = json.ToMemoryStream();
The best web hosting service is the one that not only provides cheap web hosting, but comes with perks like email hosting as well. This and the added benefits of hosting more than one domain names of course adds to the entire package. Thanks to the extensive promotion of online marketing, now anyone with access to computers knows of this. This has contributed effectively to the popularity of home business setups as well.