using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using Chernobyl.ComponentModel;
namespace Chernobyl.Creation
{
///
/// An used when an instance of some
/// type has been created by the invoker of the event.
///
public class CreationEventArgs : AsyncCompletedEventArgs
{
///
/// Initializes a new instance of the
/// class.
///
/// The instance that was created by the invoker
/// of the event.
public CreationEventArgs(object instance)
: this(instance, null)
{ }
///
/// Initializes a new instance of the
/// class.
///
/// The instance that was created by the invoker
/// of the event.
/// The optional user-supplied object used for
/// whatever purpose the user deems necessary or null if the user deems
/// this parameter as unnecessary.
public CreationEventArgs(object instance, object userState)
: this(instance, null, false, userState)
{ }
///
/// Initializes a new instance of the
/// class.
///
/// A value indicating whether the operation was
/// canceled.
public CreationEventArgs(bool cancelled)
: this(cancelled, null)
{ }
///
/// Initializes a new instance of the
/// class.
///
/// A value indicating whether the operation was
/// canceled.
/// The optional user-supplied object used for
/// whatever purpose the user deems necessary or null if the user deems
/// this parameter as unnecessary.
public CreationEventArgs(bool cancelled, object userState)
: this(null, null, cancelled, userState)
{ }
///
/// Initializes a new instance of the
/// class.
///
/// Any error that occurred during the operation.
public CreationEventArgs(Exception error)
: this(error, null)
{ }
///
/// Initializes a new instance of the
/// class.
///
/// Any error that occurred during the operation.
/// The optional user-supplied object used for
/// whatever purpose the user deems necessary or null if the user deems
/// this parameter as unnecessary.
public CreationEventArgs(Exception error, object userState)
: this(null, error, false, userState)
{ }
///
/// Initializes a new instance of the
/// class.
///
/// The instance that was created by the invoker
/// of the event.
/// Any that occurred during
/// the operation.
/// True if the operation was canceled, false
/// if otherwise.
/// The optional user-supplied object used for
/// whatever purpose the user deems necessary or null if the user deems
/// this parameter as unnecessary.
protected CreationEventArgs(object instance, Exception error, bool cancelled, object userState)
: base(error, cancelled, userState)
{
CreatedInstance = instance;
}
///
/// The instance that was created by the invoker of the event. Note that
/// this property will throw an if the
/// is set to anything but
/// null or the
/// property is set to true.
///
/// Thrown if
/// was set to anything but
/// null. The property holds a
/// reference to .
/// Thrown if
/// is set to true
/// which indicates that the operation that attempted to create the
/// instance was cancelled.
public object CreatedInstance
{
get
{
RaiseExceptionIfNecessary();
return _createdInstance;
}
protected set { _createdInstance = value; }
}
///
/// The backing field to .
///
object _createdInstance;
}
///
/// An used when an instance of some
/// type has been created by the invoker of the event.
///
/// The type of the instance that was created.
public class CreationEventArgs : CreationEventArgs
{
///
/// Initializes a new instance of the
/// class.
///
/// The instance that was created by the invoker
/// of the event.
public CreationEventArgs(TCreated instance)
: this(instance, null)
{ }
///
/// Initializes a new instance of the
/// class.
///
/// The instance that was created by the invoker
/// of the event.
/// The optional user-supplied object used for
/// whatever purpose the user deems necessary or null if the user deems
/// this parameter as unnecessary.
public CreationEventArgs(TCreated instance, object userState)
: this(instance, null, false, userState)
{ }
///
/// Initializes a new instance of the
/// class.
///
/// A value indicating whether the operation was
/// canceled.
public CreationEventArgs(bool cancelled)
: this(cancelled, null)
{ }
///
/// Initializes a new instance of the
/// class.
///
/// A value indicating whether the operation was
/// canceled.
/// The optional user-supplied object used for
/// whatever purpose the user deems necessary or null if the user deems
/// this parameter as unnecessary.
public CreationEventArgs(bool cancelled, object userState)
: this(default(TCreated), null, cancelled, userState)
{ }
///
/// Initializes a new instance of the
/// class.
///
/// Any error that occurred during the operation.
public CreationEventArgs(Exception error)
: this(error, null)
{ }
///
/// Initializes a new instance of the
/// class.
///
/// Any error that occurred during the operation.
/// The optional user-supplied object used for
/// whatever purpose the user deems necessary or null if the user deems
/// this parameter as unnecessary.
public CreationEventArgs(Exception error, object userState)
: this(default(TCreated), error, false, userState)
{ }
///
/// Initializes a new instance of the
/// class.
///
/// The instance that was created by the invoker
/// of the event.
/// Any that occurred during
/// the operation.
/// True if the operation was canceled, false
/// if otherwise.
/// The optional user-supplied object used for
/// whatever purpose the user deems necessary or null if the user deems
/// this parameter as unnecessary.
protected CreationEventArgs(TCreated instance, Exception error, bool cancelled, object userState)
: base(instance, error, cancelled, userState)
{
CreatedInstance = instance;
}
///
/// The instance that was created by the invoker of the event. Note that
/// this property will throw an if the
/// is set to anything but
/// null or the
/// property is set to true.
///
/// Thrown if
/// was set to anything but
/// null. The property holds a
/// reference to .
/// Thrown if
/// is set to true
/// which indicates that the operation that attempted to create the
/// instance was cancelled.
public new TCreated CreatedInstance
{
get { return (TCreated)base.CreatedInstance; }
protected set { base.CreatedInstance = value; }
}
}
///
/// A delegate used for indicating when a particular instance has been
/// created, when its creation was canceled, or when an error occurred
/// during its creation.
///
/// The instance that generated the event.
/// The instance
/// containing the event data.
public delegate void CreationCompletedEventHandler(Object sender, CreationEventArgs e);
///
/// A delegate used for indicating when a particular instance has been
/// created, when its creation was canceled, or when an error occurred
/// during its creation.
///
/// The instance that generated the event.
/// The instance
/// containing the event data.
public delegate void CreationCompletedEventHandler(Object sender, CreationEventArgs e);
///
/// Extension and utility methods for
/// and its dependencies.
///
public static class CreationEventArgExtensions
{
///
/// Converts the specified
/// that contains an instance of type
/// to an that contains an
/// instance of type
///
/// The type of the created instance to
/// convert from.
/// The type of the created instance to
/// convert to.
/// The instance that is to be converted.
/// The method that will convert the
/// to an
/// .
/// The converted to instance.
public static CreationEventArgs Convert(
this CreationEventArgs eventArgs,
Func convertCreation)
{
CreationEventArgs result;
if (eventArgs.Error != null)
result = new CreationEventArgs(eventArgs.Error,
eventArgs.UserState);
else if (eventArgs.Cancelled)
result = new CreationEventArgs(true,
eventArgs.UserState);
else
result = new CreationEventArgs(
convertCreation(eventArgs.CreatedInstance),
eventArgs.UserState);
return result;
}
///
/// An event handler that adds a created item to an
/// but only if the creation did not
/// get cancelled or result in an error.
///
/// The type of the item held in the
/// .
/// The type of the created item reported
/// by the . This type must be
/// of type .
/// The instance to add the created item to.
/// The sender of the event.
/// The instance
/// containing the created item.
public static void Add(this ICollection collection, object sender, CreationEventArgs e)
where TCreated : TItems
{
if (e.IsGood())
collection.Add(e.CreatedInstance);
}
}
}