|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AutoDisposeHelper.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// Auto diposes the properties that have this attribute when the property is declared.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Reflection;
using System.Windows;
namespace Catel.Windows
{
/// <summary>
/// Auto diposes the properties that have this attribute when the property is declared.
/// </summary>
/// <remarks>
/// The attribute itself only defines itself, but has no real implementation. The supported object that allows
/// to use this object should implement the actual disposing.
/// </remarks>
[AttributeUsage(AttributeTargets.Property)]
public class AutoDisposeAttribute : Attribute
{
}
/// <summary>
/// Auto dispose helper class.
/// </summary>
public static class AutoDisposeHelper
{
/// <summary>
/// Registers the AutoDisposeProperties call to the Unloaded event of the given element.
/// </summary>
/// <param name="element">The element.</param>
public static void EnableAutoDisposeProperties(this FrameworkElement element)
{
if (element != null)
{
element.Unloaded -= UnloadedWrapper;
element.Unloaded += UnloadedWrapper;
}
}
/// <summary>
/// Handles the AutoDisposeProperties call.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
private static void UnloadedWrapper(object sender, RoutedEventArgs e)
{
AutoDisposeProperties(sender);
FrameworkElement element = sender as FrameworkElement;
if (element != null)
{
element.Unloaded -= UnloadedWrapper;
}
}
/// <summary>
/// Automatically disposes all properties of the object decorated with the <see cref="AutoDisposeAttribute"/>.
/// </summary>
/// <param name="obj">The object to dispose the properties of.</param>
public static void AutoDisposeProperties(object obj)
{
if (obj == null)
{
return;
}
PropertyInfo[] properties = obj.GetType().GetProperties();
foreach (PropertyInfo propertyInfo in properties)
{
if (Attribute.IsDefined(propertyInfo, typeof(AutoDisposeAttribute)))
{
object value = propertyInfo.GetValue(obj, null);
AutoDisposeObject(value);
}
}
}
/// <summary>
/// Automatically disposes an object. If the object is an enumerable, all childs are disposed.
/// </summary>
/// <param name="obj">The object to dispose.</param>
private static void AutoDisposeObject(object obj)
{
if (obj == null)
{
return;
}
if (obj is IEnumerable)
{
foreach (object childObj in (IEnumerable)obj)
{
AutoDisposeObject(childObj);
}
}
if (obj is IDisposable)
{
((IDisposable)obj).Dispose();
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.