C# 我应该使用多个RegularExpression属性吗

C# 我应该使用多个RegularExpression属性吗,c#,C#,更新8: 这个问题有了一个新的标题,希望它能帮助其他人避免耗时的bug 我有以下代码: (您需要对System.ComponentModel.DataAnnotations的引用) 使用系统; 使用System.Collections.Generic; 使用System.ComponentModel.DataAnnotations; 名称空间控制台 { 班级计划 { 静态void Main(字符串[]参数) { var itemModelX=new MyModel(){Name=“1不要扩展Re

更新8: 这个问题有了一个新的标题,希望它能帮助其他人避免耗时的bug

我有以下代码: (您需要对System.ComponentModel.DataAnnotations的引用)

使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
名称空间控制台
{
班级计划
{
静态void Main(字符串[]参数)
{
var itemModelX=new MyModel(){Name=“1不要扩展RegularExpressionAttribute并将AllowMultiple设置为true
它只会给你带来麻烦。
您可以创建从RegularExpressionAttribute继承的两个不同属性

public class MyModel
{
    [MultipleRegExAttribute2(@"[^?.]{1,100}$")]
    [MultipleRegExAttribute3(@"^((?![><&])[\s\S])*$")]
    public string Name { get; set; }
}


public class MultipleRegExAttribute2 : RegularExpressionAttribute
{
    public MultipleRegExAttribute2(string pattern) : base(pattern) { }
}


public class MultipleRegExAttribute3 : RegularExpressionAttribute
{
    public MultipleRegExAttribute3(string pattern) : base(pattern) { }
}
公共类MyModel
{
[MultipleRegExAttribute2(@“[^?]{1100}$”)

[MultipleRegExAttribute3(@“^(?![>你试过在VS中使用调试模式测试exe吗?我疯了吗?其他人能复制它吗?@blogprogramisty.net“测试exe”是什么意思???谢谢分享此信息,但对于两个正则表达式属性,这对我来说没有意义。请改用一个正则表达式。@Ares是的,这是另一个选项,但我更喜欢这种方式,因为我可以在其他类上重用每个属性。如果我有更好的名称,它会更清晰
.locals init (
    [0] class [System.ComponentModel.DataAnnotations]System.ComponentModel.DataAnnotations.ValidationContext contextX,
    [1] class [mscorlib]System.Collections.Generic.List`1<class [System.ComponentModel.DataAnnotations]System.ComponentModel.DataAnnotations.ValidationResult> resultsX             )
.locals init (
    [0] class ConsoleApp.MyModel itemModelX,
    [1] class [System.ComponentModel.DataAnnotations]System.ComponentModel.DataAnnotations.ValidationContext contextX,
            [2] class [mscorlib]System.Collections.Generic.List`1<class [System.ComponentModel.DataAnnotations]System.ComponentModel.DataAnnotations.ValidationResult> resultsX,
            [3] bool isValidX,
            [4] class ConsoleApp.MyModel '<>g__initLocal0'
        )
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {

            var property = TypeDescriptor.GetProperties(typeof(MyModel))[0];
            var attribute = property.Attributes.Cast<Attribute>();
            foreach (var item in attribute)
                if (item is MultipleRegExAttribute2)
                    Console.WriteLine(((MultipleRegExAttribute2)item).GetPattern());
            Console.ReadLine();
        }
    }

    public class MyModel
    {
        [MultipleRegExAttribute2(@"[^?.]{1,100}$")]
        [MultipleRegExAttribute2(@"^((?![><&])[\s\S])*$")]
        public string Name { get; set; }
    }

    [System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple = true)]
    public class MultipleRegExAttribute2 : RegularExpressionAttribute
    {
        public MultipleRegExAttribute2(string pattern) : base(pattern) { }
        public string GetPattern() { return this.Pattern; }
    }
}
public class MyModel
{
    [MultipleRegExAttribute2(@"[^?.]{1,100}$")]
    [MultipleRegExAttribute3(@"^((?![><&])[\s\S])*$")]
    public string Name { get; set; }
}


public class MultipleRegExAttribute2 : RegularExpressionAttribute
{
    public MultipleRegExAttribute2(string pattern) : base(pattern) { }
}


public class MultipleRegExAttribute3 : RegularExpressionAttribute
{
    public MultipleRegExAttribute3(string pattern) : base(pattern) { }
}