using System;
using Deltares.Standard.Extensions;
namespace Deltares.Standard.Specifications
{
///
/// Defines the abstract base class for the specification attributes
/// to derive more specific types from
///
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
public abstract class SpecificationBaseAttribute : Attribute
{
private readonly ISpecification specification;
protected SpecificationBaseAttribute(Type specification, params object[] args)
{
if (args == null && !specification.HasDefaultConstructor())
throw new ArgumentException("The specification type should have a default constructor or supply ");
if (!typeof(ISpecification).IsAssignableFrom(specification))
throw new ArgumentException("The specification type should implement interface ISpecification");
this.specification = Activator.CreateInstance(specification, args) as ISpecification;
}
protected SpecificationBaseAttribute(Type specification) : this(specification, null)
{
}
///
/// Gets or sets the name of the specification.
///
/// If this property is not set, the name of the specification (when available) will be used
///
/// The name.
///
public string Name { get; set; }
///
/// Gets or sets the text in case the specification is not satisfied
///
/// If this property is not set, the description of the specification (when available) will be used
///
/// The not satisfied text.
///
public string NotSatisfiedText { get; set; }
public string GroupName { get; set; }
// This is a positional argument
public ISpecification Specification
{
get { return this.specification; }
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
public sealed class SpecificationAttribute : SpecificationBaseAttribute
{
public SpecificationAttribute(Type specification, params object[] args)
: base(specification, args)
{
}
public SpecificationAttribute(Type specification) : base(specification)
{
}
}
}