Welcome to part 2 of the Powerful Extension Methods series. Today, we take a look at XML serialization. If you have ever used the .NET XmlSerializer class, you know how powerful it is. Decorate your class with appropriate attributes, and you can generate or parse complex XML structures without having to write any XML parsing code.

To find out more about .NET and XML serialization, check out the documentation here. If you want to see excellent examples of the XmlSerializer in action, check out the code of DasBlog and look at what they do with RSS and Atom feeds. It's pretty neat.

It doesn't take long, working with the XmlSerializer, before the good ol' DRY principle drives us to create serialization helper classes, like Andrew Gunn created. Utility classes are usually perfect candidates for extension methods.

So here we go: one extension method for serializing any object (if it is not serializable, expect an exception), and a generic method each for deserializing streams, strings, and XmlNodes.

/// <summary>
/// This class provides extension methods related to serialization.
/// </summary>
public static class SerializationExtensions
{
  /// <summary>
  /// Serializes an object as XML.
  /// </summary>
  /// <param name="value">The object to serialize.</param>
  /// <returns>An XML string representing the serialized object.</returns>
  public static string SerializeAsXml(this object value)
  {
      var ser = new XmlSerializer(value.GetType());
      using (var stream = new MemoryStream())
      using (var xmlwriter = new XmlTextWriter(stream, Encoding.UTF8))
      {
          ser.Serialize(xmlwriter, value);
          var buffer = stream.GetBuffer();

          // skip the byte order mask
          return Encoding.UTF8.GetString(buffer, 3, buffer.Length - 3);
      }
  }

  /// <summary>
  /// Deserializes the specified string using an XML serializer.
  /// </summary>
  /// <typeparam name="T">The type of object to deserialize.</typeparam>
  /// <param name="stream">The stream to deserialize.</param>
  /// <returns>A deserialized object of the specified type.</returns>
  public static T Deserialize<T>(this Stream stream)
  {
    using (var reader = new XmlTextReader(reader))
    {
      var ser = new XmlSerializer(typeof(T));
      return (T)ser.Deserialize(reader);
    }
  }

  /// <summary>
  /// Deserializes the specified string using an XML serializer.
  /// </summary>
  /// <typeparam name="T">The type of object to deserialize.</typeparam>
  /// <param name="xml">The XML to deserialize.</param>
  /// <returns>A deserialized object of the specified type.</returns>
  public static T Deserialize<T>(this string xml)
  {
    using (var reader = new StringReader(xml))
    {
      var ser = new XmlSerializer(typeof(T));
      return (T)ser.Deserialize(reader);
    }
  }

  /// <summary>
  /// Deserializes the specified node using an XML serializer.
  /// </summary>
  /// <typeparam name="T">The type of object to deserialize.</typeparam>
  /// <param name="node">The node containing XML to deserialize.</param>
  /// <returns>A deserialized object of the specified type.</returns>
  public static T Deserialize<T>(this XmlNode node)
  {
    using (var reader = new StringReader(node.OuterXml))
    {
      var ser = new XmlSerializer(typeof(T));
      return (T)ser.Deserialize(reader);
    }
  }
}

Happy Coding!


 
Categories: C# | Extension Methods

Welcome to part 1 of the Powerful Extension Methods series. The extension method concept is an excellent feature of the latest breeds of the .NET framework. They allow us to abstract away static utility classes in a way that makes the utility methods appear to belong to the object they operate upon. Beginners can check things out here and here for a good introduction.

I do WinForms programming from time to time. In any Windows application, whether through traditional WinForms or through WPF, managing event handling in the face of threading is critical. Exceptions are thrown when code updates any UI Control property from any thread except the thread that created the control. This problem surfaces when some background thread needs to update the UI.

This led to a lot of creative solutions involving InvokeRequired and Invoke. One solution involves creating an event on the threaded object which the UI can subscribe to. The OnMyEvent method of the object handles invoking each event handler methods on the thread from which they subscribed. The full idea is described here.

It only took me an event handler or two before the DRY principle screamed for attention. Enter, the SafeInvoke extension method:

/// <summary>
/// Raises the event asynchronously.
/// </summary>
/// <param name="handler">The handler.</param>
/// <param name="source">The source.</param>
/// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
public static void SafeInvoke(this EventHandler handler, object source, EventArgs args)
{
  if (handler != null)
  {
    foreach (EventHandler singleCast in handler.GetInvocationList())
    {
      var syncInvoke = singleCast.Target as ISynchronizeInvoke;
      try
      {
        if (syncInvoke != null && syncInvoke.InvokeRequired)
        {
          syncInvoke.Invoke(singleCast, new object[] { source, args });
        }
        else
        {
          singleCast(source, args);
        }
      }
      catch
      {
        // Do nothing. The event handler may have been detached asynchronously.
      }
    }
  }
}

With this extension method, we can construct our threaded object to look something like this:

public class MyWorker
{
  public event EventHandler SomeEvent;

  protected virtual void OnSomeEvent(EventArgs e)
  {
    this.SomeEvent.SafeInvoke(this, e);
  }
}

You can see that using this pattern, the event will manage on its own the process of ensuring the event is handled on the subscribing threads. No more threading exceptions when updating the UI from a background worker!

Another common event delegate that deserves a similar extension method is the generic EventHandler<T> class. The model of the function is no surprise; but, here it is in full.

/// <summary>
/// Raises the event asynchronously.
/// </summary>
/// <typeparam name="T">The type of the event handler arguments</typeparam>
/// <param name="handler">The handler.</param>
/// <param name="source">The source.</param>
/// <param name="args">The arguments containing the event data.</param>
public static void SafeInvoke<T>(this EventHandler<T> handler, object source, T args)
where T : EventArgs { if (handler != null) { foreach (EventHandler<T> singleCast in handler.GetInvocationList()) { var syncInvoke = singleCast.Target as ISynchronizeInvoke; try { if (syncInvoke != null && syncInvoke.InvokeRequired) { syncInvoke.Invoke(singleCast, new object[] { source, args }); } else { singleCast(source, args); } } catch { // Do nothing. The event handler may have been detached asynchronously. } } } }

Happy Coding!


 
Categories: C# | Extension Methods