- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 DataType
Attribute 有什么区别?并传入值 DataType.Phone
和 Phone
Attribute它继承自 DataType
并自动设置 DataType.Phone
?
这两个类有什么区别吗?
class Person {
<b>[DataType(DataType.PhoneNumber)]</b>
public string PhoneNumber {get;set;}
}
class Person {
<b>[Phone]</b>
public string PhoneNumber {get;set;}
}
最佳答案
[Phone]
提供验证逻辑,而 [DataType]
不提供继承链是这样的:
属性
↳ 验证属性
↳ 数据类型属性
↳ 电话属性
所以两者都是 ValidationAttribute
的实例,但是两者都不提供开箱即用的验证。 DataType 基类仅提供用于分配 enum DataType
的结构,并将覆盖验证留给调用者
DataType
- 根据文档:When you apply the
DataTypeAttribute
attribute to a data field you must do the following:
- Issue validation errors as appropriate.
DataType
- 根据源代码:[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Parameter, AllowMultiple = false)]
public class DataTypeAttribute : ValidationAttribute
{
/// <summary> Override of <see cref="ValidationAttribute.IsValid(object)" /> </summary>
/// <remarks>This override always returns <c>true</c>. Subclasses should override this to provide the correct result.</remarks>
/// <param name="value">The value to validate</param>
/// <returns>Unconditionally returns <c>true</c></returns>
/// <exception cref="InvalidOperationException"> is thrown if the current attribute is ill-formed.</exception>
public override bool IsValid(object value)
{
EnsureValidDataType();
return true;
}
}
旁白:由于您需要覆盖 IsValid
,我不确定为什么 .NET 没有将类和属性标记为 abstract
以编程方式保证实现。
PhoneAttribute
- 验证逻辑因此,如果您确实想提供验证,并且您使用的是 .NET 4.5+ 或 .NET Core,则可以使用 [Phone]
属性,但验证机制已经改变时间也是如此,而且对于什么构成业务流程的有效输入,您可能有一套不同的规则。
.NET Framework 最初使用以下正则表达式:
const string pattern = @"^(\+\s?)?((?<!\+.*)\(\+?\d+([\s\-\.]?\d+)?\)|\d+)([\s\-\.]?(\(\d+([\s\-\.]?\d+)?\)|\d+))*(\s?(x|ext\.?)\s?\d+)?$";
但这在 .NET Framework 4.7.2 中已弃用 this change description ,可能是由于 Regular Expression Best Practices 中列出的注入(inject)/安全问题来自不受约束的输入。
如果您想继续使用正则表达式验证,您必须在 .config
的 configuration
> appsettings
部分中设置以下内容> 文件:
<add key="dataAnnotations:dataTypeAttribute:disableRegEx" value="false"/>
测试项目包括根据 PhoneAttributeTests.cs 应该通过/失败的输入示例这是一个Regexr page如果您想针对(已弃用的)正则表达式验证引擎测试匹配输入。
以下是不同 .NET 风格的源代码和文档的一些链接:
| .NET Core | .NET Core 2.1 | .NET 4.7.2 | .NET |
------------------|-------------------|----------------|-----------------|----------------|
<b>DataTypeAttribute</b> | <a href="https://github.com/dotnet/corefx/blob/v2.1.5/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/DataTypeAttribute.cs" rel="noreferrer noopener nofollow">github.com/dotnet</a> | <a href="https://source.dot.net/#System.ComponentModel.Annotations/System/ComponentModel/DataAnnotations/DataTypeAttribute.cs" rel="noreferrer noopener nofollow">source.dot.net</a> | <a href="https://referencesource.microsoft.com/#System.ComponentModel.DataAnnotations/DataAnnotations/DataTypeAttribute.cs" rel="noreferrer noopener nofollow">referencesource</a> | <a href="https://learn.microsoft.com/en-us/dotnet/api/System.ComponentModel.DataAnnotations.DataTypeAttribute" rel="noreferrer noopener nofollow">docs.microsoft</a> |
<b>PhoneAttribute</b> | <a href="https://github.com/dotnet/corefx/blob/v2.1.5/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/PhoneAttribute.cs" rel="noreferrer noopener nofollow">github.com/dotnet</a> | <a href="https://source.dot.net/#System.ComponentModel.Annotations/System/ComponentModel/DataAnnotations/PhoneAttribute.cs" rel="noreferrer noopener nofollow">source.dot.net</a> | <a href="https://referencesource.microsoft.com/#System.ComponentModel.DataAnnotations/DataAnnotations/PhoneAttribute.cs" rel="noreferrer noopener nofollow">referencesource</a> | <a href="https://learn.microsoft.com/en-us/dotnet/api/System.ComponentModel.DataAnnotations.PhoneAttribute" rel="noreferrer noopener nofollow">docs.microsoft</a> |
注意:[Phone]
的当前文档错误地指出验证使用正则表达式,自 4.7.2+ 或 .NET 中的任何地方以来都不是这样核心,所以我有submitted this PR to update
关于c# - DataType(DataType.PhoneNumber) 和 PhoneAttribute 之间有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53088476/
这个问题已经有答案了: EmailAddress or DataType.Email attribute (3 个回答) 已关闭 3 年前。 我使用了像[EmailAddress]这样的数据属性和[D
我收到此错误: "CS0104: 'DataType' is an ambiguous reference between 'System.ComponentModel.DataAnnotations
1)我目前正在尝试理解以下代码,但我无法理解 void(*func)(void) 的含义,我可以理解我正在尝试保存名为“function”的函数的地址来自 list0513,在 void 指针函数处,
我们可以以某种方式应用这种格式吗? [Display(Name = "Date of Birthday")] [DataType(DataType.Date)] public DateTime DOB
我已按照 ORMLite 文档的规定将我的类型声明为 SERIALIZABLE,但我仍然得到: ORMLite can't store unknown class interface java.io.
嗨。 这就是我想要做的: str2 = "91"; str1 = "19"; var testQuery = from c1 in str1
我在 MVC 5 中使用 ASP.NET 身份。对于密码验证,我在 AccountViewModels.cs 中有以下代码: [Required] [DataType(DataType.Passwor
我试图了解使用之间的主要区别是什么[DataType(DataType.EmailAddress)] & [EmailAddress] . 在模型类中:- public class MYViewMod
使用 DataType Attribute 有什么区别?并传入值 DataType.Phone和 Phone Attribute它继承自 DataType 并自动设置 DataType.Phone?
我的 ViewModel 中有以下字段: [DataType(DataType.Date)] [Display(Name = "Preferred date)")] publi
如果没记错的话,是[DataType(DataType.Currency)]的DataAnnotation的对应部分在 Fluent api 中是 modelBuilder.Entity.Proper
给定一个 pandas.DataFrame带有包含混合数据类型的列,例如 df = pd.DataFrame({'mixed': [pd.Timestamp('2020-10-04'), 999, '
我在电子邮件字段上使用此代码: [Required] [DataType(DataType.EmailAddress)] [Display(Name = "Email addr
我是 NodeJs 开发的新手 我正在使用带有 mysql 和 Sequelize 的 NodeJs 来创建具有这些属性的 Batch 模型。 const Batch = sequelize.defi
我想动态生成我的数据框架构我有以下内容 错误: assert isinstance(dataType, DataType), "dataType should be DataType" Asse
我是 Golang 的新手。抱歉,我仍然对以下两者之间的区别感到困惑: type 和 type = 这是一个例子: package main import "fmt" func main()
当电子邮件地址以数字开头时,DataType DataAnnotation 中 DataType.Email 的内部验证似乎失败了。当具有 1234Something@gmail.com 样式电子邮件
有需求从CSV文件中提取数据,然后将其转换为XML,最终加载到目标中。 CSV文件中的数据为 Number,Email,Email Communication,Member Type,VIN 8532
我正在使用 [DataType(DataType.Date)]关于我们模型中的属性。 如何让 Razor 添加 css 类 datepicker到所有属于 DataType.Date 的文本框特性?
我的表 CalTime 有一个时间数据类型的列 Timespent。它以 HH:MM:SS 格式存储值。请建议如何求和该列的值。 Timespent _______ 00:07:
我是一名优秀的程序员,十分优秀!