Extension Method Round Up

April 21st, 2009 by duckworth

Extension methods in c# have definitely changed my day to day development in a positive way, as they can easily add functionality that may be missing in the base class library or provide easy access to routines that you may use frequently. Many times they can just make a snippet of code more terse or readable. Here are two extension methods I came across on StackOverflow that come in handy for firing events:

static public void RaiseEvent(this EventHandler theEvent, object sender, EventArgs e)
{
    if (theEvent != null)
        theEvent(sender, e);
}
 
static public void RaiseEvent<T>(this EventHandler<T> theEvent, object sender, T e)
    where T : EventArgs
{
    if (theEvent != null)
        theEvent(sender, e);
}
 

which can be used like such:

SomthingHappenedEvent.RaiseEvent(this, new EventArgs());

another useful extension method for passing in a property name without using hardcoded strings:

public static string GetPropertyName<T, S>(this T obj, Expression<Func<T, S>> expr)
{
	return ((MemberExpression)expr.Body).Member.Name;
}

which can be used as such:

var ob = new { SomeProp = "abc"};
string propName = ob.GetPropertyName(a => a.SomeProp);

and finally, another handy set for quickly serializing any object to JSON:

public static string ToJson(this object obj)
{
    var serializer = new JavaScriptSerializer();
    return serializer.Serialize(obj);
}
 
public static string ToJson(this object obj, int recursonDepth)
{
    var serializer = new JavaScriptSerializer();
    serializer.RecursionLimit = recursonDepth;
    return serializer.Serialize(obj);
}
Bookmark and Share

Posted in C#, Code | Comments | Tags: , ,


101 Design Patterns & Tips for Developers

February 1st, 2008 by duckworth

Here is a great page to bookmark...

http://sourcemaking.com/design-patterns-and-tips

It contains an assortment/reference of design patterns, refactorings and other useful programming tips.

Bookmark and Share

Posted in Code | Comments


Last.fm

December 20th, 2007 by duckworth

What I've been listening to lately...

Bookmark and Share

Posted in Home | Comments | Tags: ,


Exporting an SSL Certificate from IIS to use in FileZilla FTP Server

January 23rd, 2007 by duckworth

FileZilla is a free, open source FTP
server (there is also a client) with SSL/TLS support.

I wanted to use my real SSL Certificate that I had for my website to secure the communication
to my FTP Server and couldn't find any instructions on how to do so. After a little
searching and some trial and error this is the solution I have come up with, I hope
someone finds this useful. Read the rest of this entry »

Bookmark and Share

Posted in Code, Server | Comments


Blingo Toolbar Extension for Firefox

June 29th, 2006 by duckworth

Blingo Blingo
is a Google-powered search engine where each search is a chance to win a prize. No
registration is required. Invite your friends to use Blingo, and if someone you invited
wins a prize by searching, we'll send you the same prize. Just use it as you would
use google.

I made a version of the Blingo toolbar for Firefox.

toolbar

The current version is .1:

I will be looking to add new features so suggestions are welcome.

Bookmark and Share

Posted in Code | Comments