Is it possible to specify that a enum property can only have a range of values?
enum Type
{
None,
One,
Two,
Three
}
class Object
{
[Allo
Same as st4hoo's solution, only with reflection. You can make it as crazy as you would like.
using System;
using System.Linq;
namespace ConceptApplication
{
static class Program
{
static void Main(string[] args)
{
var foobar = new Foobar
{
SomeProperty = Numbers.Five
};
}
}
public class Foobar
{
private static Numbers _someProperty;
[NumberRestriction(Allowed = new[] {Numbers.One, Numbers.Two})]
public Numbers SomeProperty
{
get { return _someProperty; }
set
{
RestrictionValidator.Validate(this, "SomeProperty", value);
_someProperty = value;
}
}
}
public class NumberRestriction : Attribute
{
public Numbers[] Allowed { get; set; }
}
public static class RestrictionValidator
{
public static void Validate(T sender, string propertyName, Numbers number)
{
var attrs = sender.GetType().GetProperty(propertyName).GetCustomAttributes(typeof(NumberRestriction), true);
if (attrs.OfType().Any(attr => !(attr).Allowed.Contains(number)))
throw new ArgumentOutOfRangeException();
}
}
public enum Numbers
{
One,
Two,
Three,
Four,
Five
}
}