Asp.net mvc .NET范围和StringLength属性是否可以在MVC应用程序之外使用?

Asp.net mvc .NET范围和StringLength属性是否可以在MVC应用程序之外使用?,asp.net-mvc,attributes,Asp.net Mvc,Attributes,我想使用.NET和属性对类中的各种属性进行约束。我编写了以下非常简单的代码: class AttributePlayground { [StringLength(5)] public static String StringLengthTest { get; set; } [Range(10, 20)] public static int Age { get; set; } } static void Main(string[] args) { Att

我想使用.NET和属性对类中的各种属性进行约束。我编写了以下非常简单的代码:

class AttributePlayground
{
    [StringLength(5)]
    public static String StringLengthTest { get; set; }

    [Range(10, 20)]
    public static int Age { get; set; }
}

static void Main(string[] args)
{
    AttributePlayground.StringLengthTest = "Too long!";
    AttributePlayground.Age = 34;    
}
我以为会发生错误或异常,但一切正常


我在web上看到的所有关于这些属性的示例都是在MVC上下文中显示的,但是文档中没有提到它。

正如您所知,.NET中的属性只是在编译时烘焙到程序集中的元数据。如果没有什么可以解释它们,那么,什么也不会发生

因此,以ASP.NET MVC为例,有一个验证器解释这些属性,如下所示:

class AttributePlayground
{
    [StringLength(5)]
    public String StringLengthTest { get; set; }

    [Range(10, 20)]
    public int Age { get; set; }
}

class Program
{
    static void Main()
    {
        var attributePlayground = new AttributePlayground();
        attributePlayground.StringLengthTest = "Too long!";
        attributePlayground.Age = 34;

        var context = new ValidationContext(attributePlayground, null, null);
        var errors = new List<ValidationResult>();
        if (!Validator.TryValidateObject(attributePlayground, context, errors, true))
        {
            foreach (var error in errors)
            {
                Console.WriteLine(error.ErrorMessage);
            }
        }
    }
}
class AttributePlayground
{
[第(5)款]
公共字符串StringLengthTest{get;set;}
[射程(10,20)]
公共整数{get;set;}
}
班级计划
{
静态void Main()
{
var attributeplayland=新attributeplayland();
attributePlayground.StringLengthTest=“太长!”;
attributePlayground.年龄=34岁;
var context=新的ValidationContext(attributePlayground,null,null);
var errors=新列表();
if(!Validator.TryValidateObject(attributePlayground、上下文、错误、true))
{
foreach(错误中的var错误)
{
Console.WriteLine(error.ErrorMessage);
}
}
}
}

但老实说,如果您打算做一些更严肃和复杂的验证,我建议您不要使用声明性验证逻辑,这就是数据注释。我推荐你。它允许您以一种很好的方式表达更复杂的验证规则,否则很难通过数据注释实现。

正如您所知,.NET中的属性只是在编译时烘焙到程序集中的元数据。如果没有什么可以解释它们,那么,什么也不会发生

因此,以ASP.NET MVC为例,有一个验证器解释这些属性,如下所示:

class AttributePlayground
{
    [StringLength(5)]
    public String StringLengthTest { get; set; }

    [Range(10, 20)]
    public int Age { get; set; }
}

class Program
{
    static void Main()
    {
        var attributePlayground = new AttributePlayground();
        attributePlayground.StringLengthTest = "Too long!";
        attributePlayground.Age = 34;

        var context = new ValidationContext(attributePlayground, null, null);
        var errors = new List<ValidationResult>();
        if (!Validator.TryValidateObject(attributePlayground, context, errors, true))
        {
            foreach (var error in errors)
            {
                Console.WriteLine(error.ErrorMessage);
            }
        }
    }
}
class AttributePlayground
{
[第(5)款]
公共字符串StringLengthTest{get;set;}
[射程(10,20)]
公共整数{get;set;}
}
班级计划
{
静态void Main()
{
var attributeplayland=新attributeplayland();
attributePlayground.StringLengthTest=“太长!”;
attributePlayground.年龄=34岁;
var context=新的ValidationContext(attributePlayground,null,null);
var errors=新列表();
if(!Validator.TryValidateObject(attributePlayground、上下文、错误、true))
{
foreach(错误中的var错误)
{
Console.WriteLine(error.ErrorMessage);
}
}
}
}
但老实说,如果您打算做一些更严肃和复杂的验证,我建议您不要使用声明性验证逻辑,这就是数据注释。我推荐你。它允许您以一种很好的方式表达更复杂的验证规则,否则使用数据注释将很难实现这一点