2016-02-26 53 views
1

更新8: 這個問題有一個新的標題,希望這將幫助其他避免耗時的錯誤......我應該使用多個正則表達式屬性

我有以下代碼: (你需要一個參考到System.ComponentModel.DataAnnotations)

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 

namespace ConsoleApp 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var itemModelX = new MyModel() { Name = "1<d" }; 

      var contextX = new ValidationContext(itemModelX, serviceProvider: null, items: null); 
      var resultsX = new List<ValidationResult>(); 
      var isValidX = Validator.TryValidateObject(itemModelX, contextX, resultsX, true); 

      if (isValidX) 
       Console.WriteLine("Should not see this"); 

      Console.WriteLine("Finished"); 
      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) { } 
    } 
} 

在Visual Studio 2013高級更新5的輸出是

「不應該看到這個」
「完成」

在Visual Studio 2015年企業更新1個輸出

「完成」

羅斯林是正確的,在問題是爲什麼2013年不起作用?
我敢肯定的是,在2013工作也,不知道什麼時候就壞了,我沒有以前的更新,以測試它...
我使用.NET 4.5.1

更新1
即使我刪除提及System.ComponentModel.DataAnnotations並把代碼添加到我的項目(你可以找到的代碼here)我得到不同的輸出。

更新2
忘了說,它只是如果我有2個MultipleRegExAttributes發生,如果我刪除它按預期工作

更新3 我上傳的整個解決方案here第一個

更新4 我檢查兩個程序集生成的IL,但我看到的唯一區別是幾個本地人的初始化

VS2015

.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   ) 

VS2013

.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' 
     ) 

更新5
幾乎沒有...

  • VS2015只檢查第二屬性(Happe的ns是我在 關心的情況下)
  • VS2013只檢查第一個參數...

更新6
很多很多小時後...
- 它似乎總是存在,如果我有一個問題,超過1 RegularExpressionAttribute
這可能是我的錯,因爲我擴展類和「覆蓋「AllowMultiple

2.當我用VS2015編譯時,爲什麼會得到不同的結果?
返回屬性的類是方法GetAttributes中的System.ComponentModel.TypeDescriptor。
我要看看是什麼改變那裏...

更新7
似乎有什麼東西在方式的改變的PropertyDescriptor/MemberDescriptor返回屬性。
在VS2015中打印第二個正則表達式,在VS2013中打印第一個正則表達式。
那麼,以下哪一項是正確的?

  • 一)這是一個實現細節,我不應該依賴於這個
  • B)這是一個錯誤,因爲它是其他重大更改
  • C)A和B
  • d)

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; } 
    } 
} 
+0

你有沒有嘗試測試中使用VS調試模式下的EXE這一翻譯? –

+0

我瘋了嗎?其他人可以重現嗎? –

+0

@ blogprogramisty.net你是什麼意思「測試exe」? –

回答

1

不要延長RegularExpressionAttribute並設置到的AllowMultiple真正
它會給你帶來什麼,但麻煩。

您可以創建2個從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) { } 
} 

更新

我的一個朋友告訴我的問題的根源。
我必須重寫TypeId屬性。
看到這個問題:Custom validation attribute with multiple instances problem
和這篇文章:The Importance of TypeId in ASP.NET MVC DataAnnotations Validation Attributes

+0

感謝您分享此信息,但對於兩個正則表達式屬性,它對我來說沒有意義。改爲使用單個正則表達式。 – Ares

+1

@Ares是的,這是另一種選擇,但我更喜歡這種方式,因爲我可以在其他類上重用每個屬性。如果我有更好的名稱,它會更清晰 –