当前位置: 首页 > 编程语言 > C#

我应该使用MultipleRegularExpression属性吗?Share

时间:2023-04-10 14:22:10 C#

我应该使用MultipleRegularExpression属性吗?更新8:这个问题有一个新标题,希望它能帮助其他人避免耗时的错误......我有以下代码:(你需要参考System.ComponentModel.DataAnnotations)usingSystem;使用System.Collections.Generic;使用System.ComponentModel.DataAnnotations;namespaceConsoleApp{classProgram{staticvoidMain(string[]args){varitemModelX=newMyModel(){Name="1<&])[sS])*$")]公共字符串名字{得到;放;}}[System.AttributeUsage(System.AttributeTargets.Field|System.AttributeTargets。Property,AllowMultiple=true)]publicclassMultipleRegExAttribute2:RegularExpressionAttribute{publicMultipleRegExAttribute2(stringpattern):base(pattern){}}}在VisualStudio2013PremiumUpdate5中,在“成品”中输出“不应该看到这个””在VisualStudio2015EnterpriseUpdate1中,输出是“成品”Roslyn是正确的,问题是为什么2013不起作用?我很确定这在2013年也能正常工作,不知道它什么时候坏了,我没有以前的更新来测试它...我正在使用.NET4.5.1更新1,即使我删除了引用System.ComponentModel.DataAnnotations并将代码添加到我的项目中(您可以在此处找到代码),我得到了不同的输出更新2忘了说只有当我有2个MultipleRegExAttributes时才会发生,如果我删除第一个它会按预期工作更新3我在此处上传了整个解决方案更新4我检查了两个程序集生成的IL,但我看到的唯一区别是几个局部变量的初始化VS2015.localsinit([0]class[System.ComponentModel.DataAnnotations]System.ComponentModel.DataAnnotations.ValidationContextcontextX,[1]class[mscorlib]System.Collections.Generic.List`1resultsX)VS2013。localsinit([0]classConsoleApp.MyModelitemModelX,[1]class[System.ComponentModel.DataAnnotations]System.ComponentModel.DataAnnotations.ValidationContextcontextX,[2]class[mscorlib]System.Collections.Generic.List`1resultsX,[3]boolisValidX,[4]classConsoleApp.MyModel'g__initLocal0')Update5差不多了...Update6几个小时后...-如果我有超过1个RegularExpressionAttribute似乎总是有问题可能是我的错误是因为我扩展了类并“覆盖”了AllowMultiple2.为什么我用VS2015编译它时会得到不同的结果?返回属性的类是方法GetAttributes中的System.ComponentModel.TypeDescriptor。我将不得不看看那里发生了什么变化......更新7PropertyDescriptor/MemberDescriptor返回属性的方式似乎已经改变。在VS2015中它打印第二个正则表达式,在VS2013中它打印第一个。那么,以下哪项是正确的?.使用系统;使用System.ComponentModel;使用System.ComponentModel.DataAnnotations;使用System.Linq;namespaceConsoleApp{classProgram{staticvoidMain(string[]args){varproperty=TypeDescriptor.GetProperties(typeof(MyModel))[0];varattribute=property.Attributes.Cast();foreach(variteminattribute)if(itemisMultipleRegExAttribute2)Console.WriteLine(((MultipleRegExAttribute2)item).GetPattern());控制台.ReadLine();}}publicclassMyModel{[MultipleRegExAttribute2(@[^?.]{1,100}$")][MultipleRegExAttribute2(@"^((?![><&])[sS])*$")]公共字符串名字{得到;放;}}[System.AttributeUsage(System.AttributeTargets.Field|System.AttributeTargets.Property,AllowMultiple=true)]publicclassMultipleRegExAttribute2:RegularExpressionAttribute{publicMultipleRegExAttribute2(stringpattern):base(pattern){}publicstringGetPattern(){返回这个。模式;}}}不要扩展RegularExpressionAttribute并将AllowMultiple设置为true它会给你带来麻烦你可以创建2个不同的属性继承自RegularExpressionAttribute。publicclassMyModel{[MultipleRegExAttribute2(@"[^?.]{1,100}$")][MultipleRegExAttribute3(@"^((?![>UPDATE我的一个朋友向我展示了问题的根源。我不得不重写TypeId看到这个问题:Customvalidationattributeswithmultipleinstanceissues和这篇文章:ImportanceofTypeMdinASP.NETMVCDataAnnotationsvalidationattributes以上是C#学习教程:ShouldIuseMultipleRegularExpressionattributes?分享完整内容,如果是对你有用,需要了解更多C#学习教程,希望大家多多关注---本文收集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除,如有转载请注明出处: