Click here to Skip to main content
6,594,932 members and growing! (15,611 online)
Email Password   helpLost your password?
Languages » C# » How To     Beginner License: The Code Project Open License (CPOL)

Extensions - what can I do with them

By Zakir Hoosen

What are the possibilities of extension classes? See how you use them to provide logging facilities automagically
C# (C# 1.0, C# 2.0, C# 3.0), .NET (.NET 3.5), Architect, Design
Posted:29 Sep 2008
Views:3,443
Bookmarked:9 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 2.07 Rating: 2.44 out of 5
2 votes, 28.6%
1
1 vote, 14.3%
2
2 votes, 28.6%
3
1 vote, 14.3%
4
1 vote, 14.3%
5

Extensions… are they useful?

With the introduction of .net 3.5 along came extensions. Extension classes are a way of extending types by adding functionality, even to types you don’t have the source code to.
With that we can extend for example the string to validate if a string is a valid email address.

public static bool IsValidEmailAddress(this string s)
{
   Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
   return regex.IsMatch(s); 
}

and to use it, we do…

bool isEmail = teststring.IsValidEmailAddress();

Within extension classes we can also use generics, so we could extend system.object to return all the public properties of an object.

public static List<PropertyInfo> Properties<T>(this T item)
{
    List<PropertyInfo> list = new List<PropertyInfo>();
    foreach (PropertyInfo property in item.GetType().GetProperties())
        list.Add(property);
    return list;
}

And the usage will be…

object testObj = new object();
foreach (PropertyInfo property in testObj.Properties())
    /*.do something*/

Real world

Ok, so how’s this really beneficial to us . Let’s attempt a real world scenario.

A common requirement in modern applications is the ability to log information, log exceptions and warnings.  We’re going to create a basic logging solution and add an extension method to make logging simpler and available to ALL exceptions in our application.

below is the code for the logging solution. Its just some sample code to get us through the article.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fuzzelogic.Samples.Logging
{
    public interface ILogProvider
    {
        void Log(LoggingTypes type, string message);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Fuzzelogic.Samples.Logging
{
    public enum LoggingTypes
    {
        Warning,
        Information,
        Exception
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; 

namespace Fuzzelogic.Samples.Logging
{
    public class FileLogger : ILogProvider
    {
        string _FileName = "";
        public FileLogger(string fileName)
        {
            _FileName = fileName;
        }

#region ILogProvider Members
        public void Log(LoggingTypes type, string message)
        {
            string formattedMessage = string.Format ( "{0} : {1}\n", type.ToString(), 
                                                      message );
            try
            {
                File.AppendAllText(_FileName, formattedMessage);
            }
            catch
            {
                // Do nothing.
            }
        }
#endregion
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fuzzelogic.Samples.Logging
{
    public class Logger
    {
        static List _Providers = new List();

        public static void Register(ILogProvider provider)
        {
            if (!_Providers.Contains(provider))
                _Providers.Add(provider);
        }

        public static void Log(LoggingTypes type, string message)
        {
            foreach (ILogProvider provider in _Providers)
                provider.Log(type, message);
        }
    }
}

Now here’s the exciting part.

First we create the extension class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fuzzelogic.Samples.Logging;
 
namespace Fuzzelogic.Samples.Extensions
{
    public static class ExceptionExtensions
    {
        public static void Log(this Exception exception)
        {
            string message = string.Format("{0}\n{1}", exception.Message, 
                                           exception.ToString());
            Logger.Log(LoggingTypes.Exception, message); 
        }
    }
}

OK, so now we have the logging solution, the extension class, and here’s how we now log exceptions.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fuzzelogic.Samples.Extensions;
 
namespace Fuzzelogic.Samples.TestCode
{
    public class LoggingTests
    {
        public void TestMethod()
        {
            try
            {
                /* Do something */
            }
            catch (Exception exception)
            {
                exception.Log(); // Thats all thats needed on ALL exception objects.
                throw;
            }
        }
    }
}

Hmm… so how about make the logging go further. How about the following extension…

public static class ObjectExtensions
{
    public static void Log(this object anObject, LoggingTypes type, string message)
    {
        string logMessage = string.Format("{0}:{1}", DateTime.Now.ToString(), message);
        Logger.Log(type, logMessage);
    }
}

So now every object has the ability to log information and warnings, even exceptions.

object someThing = new object();
someThing.Log(LoggingTypes.Information, "Some information message");

I’m sure there’s more to extensions, but for now the possibilities are bright . Hope this helps.

fuzzelogic Solutions.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Zakir Hoosen


Member
You can read my blog at www.fuzzelogicSolutions.com/wordpress
Occupation: Software Developer (Senior)
Company: www.fuzzelogicSolutions.com
Location: United Kingdom United Kingdom

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Sep 2008
Editor: Chris Maunder
Copyright 2008 by Zakir Hoosen
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project