What are you looking for:
Enum.IsDefined(typeof(MyEnum), entity.EnumValue)
[Update + 1]
A validator that performs many checks, including this one, is called EnumDataType. Make sure you set validateAllProperties = true as ValidateObject, otherwise your test will fail.
If you just want to check if an enumeration is defined, you can use a special validator with the line above:
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false)] public sealed class EnumValidateExistsAttribute : DataTypeAttribute { public EnumValidateExistsAttribute(Type enumType) : base("Enumeration") { this.EnumType = enumType; } public override bool IsValid(object value) { if (this.EnumType == null) { throw new InvalidOperationException("Type cannot be null"); } if (!this.EnumType.IsEnum) { throw new InvalidOperationException("Type must be an enum"); } if (!Enum.IsDefined(EnumType, value)) { return false; } return true; } public Type EnumType { get; set; } }
... but I suppose it's not out of the box, right?
atlaste
source share