using System;
using Chernobyl.Collections.Generic.Event;
using Chernobyl.Creation;
namespace Chernobyl.Plugin
{
///
/// The default implementation. This class
/// makes it easier to create new s.
///
public abstract class PluginBuilder : Builder, IPluginBuilder
{
///
/// Initializes a new instance of the class.
///
/// The instance
/// that gives and takes services and is given to the created plug-ins
/// if they have a constructor that takes an
/// instance.
protected PluginBuilder(IEventCollection services)
{
Predicate = DefaultPredicate;
Creator = new DefaultCreator(services);
}
///
/// The method used to choose which types to create.
///
public Predicate Predicate { get; set; }
///
/// The method used to create an instance of the Type passed in. The
/// default value of this property is an instance of
/// .
///
public Func Creator { get; set; }
///
/// A class that holds the default method for property.
///
public class DefaultCreator
{
///
/// Initializes a new instance of the class.
///
/// The instance that
/// gives and takes services and is given to the created plug-ins
/// if they have a constructor that takes an
/// instance.
public DefaultCreator(IEventCollection services) { Services = services; }
///
/// The default method for the
/// property. This method checks for a constructor on the passed in type
/// that takes an instance. If one isn't found
/// then the instance is created using it's parameterless constructor.
///
/// The type to create an instance of.
/// The created instance.
public object Creator(Type type)
{
return type.GetConstructor(new[] { typeof(IEventCollection) }) != null ?
Activator.CreateInstance(type, Services) : Activator.CreateInstance(type);
}
///
/// Performs an implicit conversion from
/// to .
///
/// The to
/// convert.
/// The result of the conversion.
public static implicit operator Func(DefaultCreator defaultCreator)
{
return defaultCreator.Creator;
}
///
/// The instance that
/// gives and takes services and is given to the created plug-ins
/// if they have a constructor that takes an
/// instance.
///
public IEventCollection Services { get; protected set; }
}
///
/// The default value of the property. The
/// default predicate returns true if the Type passed in implements the
/// interface.
///
public static bool DefaultPredicate(Type type)
{
return typeof(IPlugin).IsAssignableFrom(type);
}
}
}