#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
namespace Nuclex.Support.Plugins {
/// Employer that directly creates instances of the types in a plugin
/// Interface or base class required for the employed types
///
///
/// This employer directly creates an instance of any type in a plugin assembly that
/// implements or is derived from the type the generic InstanceEmployer is instanced
/// to. This is useful when the plugin user already has a special plugin interface
/// through which additional informations about a plugin type can be queried or
/// when actually exactly one instance per plugin type is wanted (think of the
/// prototype pattern for example)
///
///
/// Because this employer blindly creates an instance of any compatible type found
/// in a plugin assembly it should be used with care. If big types with high
/// construction time or huge memory requirements are loaded this can become
/// a real resource hog. The intention of this employer was to let the plugin user
/// define his own factory interface which possibly provides further details about
/// the type the factory is reponsible for (like a description field). This
/// factory would then be implemented on the plugin side.
///
///
public class InstanceEmployer : Employer {
/// Initializes a new instance employer
public InstanceEmployer() {
this.employedInstances = new List();
}
/// All instances that have been employed
public List Instances {
get { return this.employedInstances; }
}
/// Determines whether the type suites the employer's requirements
/// Type that is checked for employability
/// True if the type can be employed
public override bool CanEmploy(Type type) {
return
PluginHelper.HasDefaultConstructor(type) &&
typeof(T).IsAssignableFrom(type) &&
!type.ContainsGenericParameters;
}
/// Employs the specified plugin type
/// Type to be employed
public override void Employ(Type type) {
this.employedInstances.Add((T)Activator.CreateInstance(type));
}
/// All instances employed by the instance employer
private List employedInstances;
}
} // namespace Nuclex.Support.Plugins