Extension Methods

F# – Make extension methods visible to C#

In F# you can write an exten­sion method like this: Whilst this will work per­fect­ly fine in your F# code, the exten­sion method will not be vis­i­ble to any C# code using the File­In­fo type because F# and C# com­piles exten­sion meth­ods dif­fer­ent­ly. To make C#-compatible exten­sion meth­ods in F#, here’s what you need to …

F# – Make exten­sion meth­ods vis­i­ble to C#Read More »

Extension Methods – Serialize/Deserialize as Json/XML

Cranked these out last night, hope you find it use­ful too 1: pub­lic sta­t­ic class Seri­al­iza­tionEx­ten­sions 2: { 3: pri­vate sta­t­ic read­on­ly ConcurrentDictionary<Type, XmlSe­ri­al­iz­er> XmlSe­ri­al­iz­ers = 4: new ConcurrentDictionary<Type, XmlSe­ri­al­iz­er>(); 5: 6: pri­vate sta­t­ic read­on­ly ConcurrentDictionary<Type, Dat­a­Con­trac­tJ­son­Se­ri­al­iz­er> Json­Se­ri­al­iz­ers = 7: new ConcurrentDictionary<Type, Dat­a­Con­trac­tJ­son­Se­ri­al­iz­er>(); 8: 9: pri­vate sta­t­ic read­on­ly JavaScript­Se­ri­al­iz­er JavaScript­Se­ri­al­iz­er = new JavaScript­Se­ri­al­iz­er(); 10: 11: …

Exten­sion Meth­ods – Serialize/Deserialize as Json/XMLRead More »

Extension Methods – Helpful parse methods for string

1: /// <sum­ma­ry> 2: /// Pars­es a string as a short 3: /// </summary> 4: pub­lic sta­t­ic short ParseShort(this string str, bool throwOnOver­flow = true) 5: { 6: return str.ParseAsStruct(Convert.ToInt16, throwOnOver­flow); 7: } 8:  9: /// <sum­ma­ry> 10: /// Pars­es a string as an ushort 11: /// </summary> 12: pub­lic sta­t­ic ushort ParseUshort(this string str, …

Exten­sion Meth­ods – Help­ful parse meth­ods for stringRead More »

IDictionary<string, object> to ExpandoObject extension method

As you know, the ExpandoOb­ject class imple­ments the IDictionary<string, object> inter­face, so if you have an ExpandoOb­ject you can eas­i­ly cast it to an IDictionary<string, object> but there’s no built-in way to eas­i­ly do the reverse. Luck­i­ly, I came across a very use­ful exten­sion method today which con­verts an IDictionary<string, object> into an ExpandoOb­ject, which …

IDictionary<string, object> to ExpandoOb­ject exten­sion methodRead More »

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

Ever tried to use IEnumerable<T>.Sum on an array of unsigned long inte­gers? Well, you can’t, because the Sum method has not been imple­ment­ed for ulong or ulong?, so to fill in the gap here’s the exten­sion meth­ods you need using more or less the same code as the exist­ing Sum meth­ods: I used some cus­tom …

Exten­sion meth­ods to sum IEnumerable(ulong) and IEnumerable(ulong?)Read More »

C# Extension method for checking attributes

Anoth­er good ques­tion on Stack­Over­flow, and even bet­ter answer from Steven (miles bet­ter than what I man­aged!), the ques­tion was around how to imple­ment an exten­sion method to check whether a cer­tain method has a par­tic­u­lar attribute applied to it, main­ly for the pur­pose of unit test­ing. In his answer, Steven pro­vid­ed a cou­ple of …

C# Exten­sion method for check­ing attrib­ut­esRead More »

Enumerable.Append and Enumerable.Prepend extension methods

Anoth­er gem I found on Stack­Over­flow today (I’ve been spend­ing a lot of time there these last cou­ple of days..), this time in the form of a ques­tion on how to append or prepend a sin­gle val­ue to an IEnumerable<T>. Greg pro­vid­ed an ele­gant solu­tion to this par­tic­u­lar prob­lem, and here’s his answer: Which you …

Enumerable.Append and Enumerable.Prepend exten­sion meth­odsRead More »

.Net Tips — String.ToTitleCase() extension methods

As you most like­ly know already, there are ToLow­er() and ToUp­per() meth­ods on the String class in C#, but a method to con­vert a string to ‘Title Case’ is sad­ly miss­ing. Sup­port for this use­ful con­ver­sion exists in the Tex­tIn­fo class which has a ToTi­tle­Case method, but you can’t instan­ti­ate a new instance of the …

.Net Tips — String.ToTitleCase() exten­sion meth­odsRead More »

Extension methods for compressing/decompressing string

Seri­al­iza­tion Over­head When it comes to serializing/deserializing objects for trans­port through the wire, you will most like­ly incur some over­head in the seri­al­ized mes­sage though the amount of over­head varies depends on the data inter­change for­mat used — XML is over­ly ver­bose where as JSON is much more light-weight: XML rep­re­sen­ta­tion: JSON rep­re­sen­ta­tion: {“MyProperty”:10} As …

Exten­sion meth­ods for compressing/decompressing stringRead 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