Mik*_*ike 13 c# enums data-annotations
C#枚举值不仅限于其定义中列出的值,还可以存储其基类型的任何值.如果未定义基本类型Int32或仅int使用基本类型.
我正在开发一个WCF服务,需要确信某些枚举具有一个值,而不是所有枚举为0的默认值.我从一个单元测试开始,找出是否[Required]能在这里做正确的工作.
using System.ComponentModel.DataAnnotations;
using Xunit;
public enum MyEnum
{
// I always start from 1 in order to distinct first value from the default value
First = 1,
Second,
}
public class Entity
{
[Required]
public MyEnum EnumValue { get; set; }
}
public class EntityValidationTests
{
[Fact]
public void TestValidEnumValue()
{
Entity entity = new Entity { EnumValue = MyEnum.First };
Validator.ValidateObject(entity, new ValidationContext(entity, null, null));
}
[Fact]
public void TestInvalidEnumValue()
{
Entity entity = new Entity { EnumValue = (MyEnum)(-126) };
// -126 is stored in the entity.EnumValue property
Assert.Throws<ValidationException>(() =>
Validator.ValidateObject(entity, new ValidationContext(entity, null, null)));
}
}
Run Code Online (Sandbox Code Playgroud)
它没有,第二次测试不会抛出任何异常.
我的问题是:是否有一个验证器属性来检查提供的值是否在Enum.GetValues?
更新.确保使用ValidateObject(Object, ValidationContext, Boolean)with last参数等于True而不是ValidateObject(Object, ValidationContext)在我的单元测试中完成.
quj*_*jck 20
有EnumDataType在.NET4 + ...
确保validateAllProperties=true在调用中设置第3个参数ValidateObject
所以从你的例子:
public class Entity
{
[EnumDataType(typeof(MyEnum))]
public MyEnum EnumValue { get; set; }
}
[Fact]
public void TestInvalidEnumValue()
{
Entity entity = new Entity { EnumValue = (MyEnum)(-126) };
// -126 is stored in the entity.EnumValue property
Assert.Throws<ValidationException>(() =>
Validator.ValidateObject(entity, new ValidationContext(entity, null, null), true));
}
Run Code Online (Sandbox Code Playgroud)
atl*_*ste 10
你在寻找的是:
Enum.IsDefined(typeof(MyEnum), entity.EnumValue)
Run Code Online (Sandbox Code Playgroud)
[更新+ 1]
开箱即用的验证器可以进行很多验证,包括这个验证,称为EnumDataType.确保将validateAllProperties = true设置为ValidateObject,否则您的测试将失败.
如果您只想检查是否定义了枚举,可以使用上述行的自定义验证器:
[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;
}
}
Run Code Online (Sandbox Code Playgroud)
......但我想它不是开箱即用的呢?