【问题标题】:Using MaxLength attribute in .Net Standard 1.2在 .Net Standard 1.2 中使用 MaxLength 属性
【发布时间】:2020-06-05 07:56:43
【问题描述】:
我的主要项目被困在 .Net 框架 4.5.1 中,并引用了 .Net Standard 中的一个项目。
出于兼容性原因,我无法将 .Net Standard 升级到 1.2 以上。
.Net Standard 项目需要从System.ComponentModel.DataAnnotations 声明具有[MaxLength()] 属性的模型,但该属性仅在2.0 版本中可用。
在标准 1.2 中有 [MaxLength()] 属性的解决方法吗?
编辑
我已经尝试自己实现它,但正如我所料,上层实体框架层没有考虑到它。
【问题讨论】:
标签:
c#
.net-standard
.net-framework-version
maxlength
【解决方案2】:
你可以自己写)
public class MyMaxLenght : ValidationAttribute
{
private int _lenght;
public MyMaxLenght(int lenght)
{
_lenght = lenght;
}
public override bool IsValid(object value)
{
if (value != null)
{
return value.ToString().Length > _lenght ? true : false;
}
return false;
}
}
使用:
public class Test
{
[MyMaxLenght(2)]
public string Name;
}
或者使用
[最大长度()]
来自
使用 System.ComponentModel.DataAnnotations;