Extension Methods

Extension Methods – Serialize/Deserialize as Json/XML

Cranked these out last night, hope you find it useful too 1: public static class SerializationExtensions 2: { 3: private static readonly ConcurrentDictionary<Type, XmlSerializer> XmlSerializers = 4: new ConcurrentDictionary<Type, XmlSerializer>(); 5: 6: private static readonly ConcurrentDictionary<Type, DataContractJsonSerializer> JsonSerializers = 7: new ConcurrentDictionary<Type, DataContractJsonSerializer>(); 8: 9: private static readonly JavaScriptSerializer JavaScriptSerializer = new JavaScriptSerializer(); 10: 11: …

Extension Methods – Serialize/Deserialize as Json/XML Read More »

Extension Methods – Helpful parse methods for string

1: /// <summary> 2: /// Parses a string as a short 3: /// </summary> 4: public static short ParseShort(this string str, bool throwOnOverflow = true) 5: { 6: return str.ParseAsStruct(Convert.ToInt16, throwOnOverflow); 7: } 8:  9: /// <summary> 10: /// Parses a string as an ushort 11: /// </summary> 12: public static ushort ParseUshort(this string str, …

Extension Methods – Helpful parse methods for string Read More »

IDictionary<string, object> to ExpandoObject extension method

As you know, the ExpandoObject class implements the IDictionary<string, object> interface, so if you have an ExpandoObject you can easily cast it to an IDictionary<string, object> but there’s no built-in way to easily do the reverse. Luckily, I came across a very useful extension method today which converts an IDictionary<string, object> into an ExpandoObject, which …

IDictionary<string, object> to ExpandoObject extension method Read More »

Extension methods to sum IEnumerable(ulong) and IEnumerable(ulong?)

Ever tried to use IEnumerable<T>.Sum on an array of unsigned long integers? Well, you can’t, because the Sum method has not been implemented for ulong or ulong?, so to fill in the gap here’s the extension methods you need using more or less the same code as the existing Sum methods: I used some custom …

Extension methods to sum IEnumerable(ulong) and IEnumerable(ulong?) Read More »

Enumerable.Append and Enumerable.Prepend extension methods

Another gem I found on StackOverflow today (I’ve been spending a lot of time there these last couple of days..), this time in the form of a question on how to append or prepend a single value to an IEnumerable<T>. Greg provided an elegant solution to this particular problem, and here’s his answer: Which you …

Enumerable.Append and Enumerable.Prepend extension methods Read More »

Extension methods for compressing/decompressing string

Serialization Overhead When it comes to serializing/deserializing objects for transport through the wire, you will most likely incur some overhead in the serialized message though the amount of overhead varies depends on the data interchange format used – XML is overly verbose where as JSON is much more light-weight: XML representation: JSON representation: {“MyProperty”:10} As …

Extension methods for compressing/decompressing string Read More »

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close