首页 新闻 搜索 专区 学院

关于RequireAttribute特性的使用这样错了吗?

0
[已解决问题] 解决于 2010-12-07 16:10
<pre>我的代码如下:</pre> <pre><br /></pre> <pre><br /></pre> <pre><div class="cnblogs_code"><pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System;<br /></span><span style="color: #008080;"> 2</span> <span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Collections.Generic;<br /></span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Text;<br /></span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.ComponentModel.DataAnnotations;<br /></span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.ComponentModel;<br /></span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Text.RegularExpressions;<br /></span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Globalization;<br /></span><span style="color: #008080;"> 8</span> <span style="color: #000000;"><br /></span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> Attribute_ConsApp_Demo<br /></span><span style="color: #008080;">10</span> <span style="color: #000000;">{<br /></span><span style="color: #008080;">11</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Program<br /></span><span style="color: #008080;">12</span> <span style="color: #000000;"> {<br /></span><span style="color: #008080;">13</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Main(</span><span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)<br /></span><span style="color: #008080;">14</span> <span style="color: #000000;"> {<br /></span><span style="color: #008080;">15</span> <span style="color: #000000;"> HomeModels home </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> HomeModels();<br /></span><span style="color: #008080;">16</span> <span style="color: #000000;"> home.Name </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;">;<br /></span><span style="color: #008080;">17</span> <span style="color: #000000;"> home.Email </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">asdfasdfasd</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br /></span><span style="color: #008080;">18</span> <span style="color: #000000;"> Console.WriteLine(home.Name</span><span style="color: #000000;">+</span><span style="color: #000000;">home.Email);<br /></span><span style="color: #008080;">19</span> <span style="color: #000000;"> Console.ReadLine();<br /></span><span style="color: #008080;">20</span> <span style="color: #000000;"> }<br /></span><span style="color: #008080;">21</span> <span style="color: #000000;"> }<br /></span><span style="color: #008080;">22</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">23</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">24</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> HomeModels<br /></span><span style="color: #008080;">25</span> <span style="color: #000000;"> {<br /></span><span style="color: #008080;">26</span> <span style="color: #000000;"> [Required(ErrorMessage</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">出错了</span><span style="color: #800000;">"</span><span style="color: #000000;">)]<br /></span><span style="color: #008080;">27</span> <span style="color: #000000;"> [DisplayName(</span><span style="color: #800000;">"</span><span style="color: #800000;">姓名</span><span style="color: #800000;">"</span><span style="color: #000000;">)]<br /></span><span style="color: #008080;">28</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">string</span><span style="color: #000000;"> Name { </span><span style="color: #0000ff;">get</span><span style="color: #000000;">; </span><span style="color: #0000ff;">set</span><span style="color: #000000;">; }<br /></span><span style="color: #008080;">29</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">30</span> <span style="color: #000000;"> [Required(ErrorMessage </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">出错了</span><span style="color: #800000;">"</span><span style="color: #000000;">)]<br /></span><span style="color: #008080;">31</span> <span style="color: #000000;"> [ValidateEmailAttribute] </span><span style="color: #008000;">//</span><span style="color: #008000;">自己加的特性</span><span style="color: #008000;"><br /></span><span style="color: #008080;">32</span> <span style="color: #000000;"> [DisplayName(</span><span style="color: #800000;">"</span><span style="color: #800000;">邮件</span><span style="color: #800000;">"</span><span style="color: #000000;">)]<br /></span><span style="color: #008080;">33</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">string</span><span style="color: #000000;"> Email { </span><span style="color: #0000ff;">get</span><span style="color: #000000;">; </span><span style="color: #0000ff;">set</span><span style="color: #000000;">; }<br /></span><span style="color: #008080;">34</span> <span style="color: #000000;"> }<br /></span><span style="color: #008080;">35</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">36</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">sealed</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> ValidateEmailAttribute : ValidationAttribute<br /></span><span style="color: #008080;">37</span> <span style="color: #000000;"> {<br /></span><span style="color: #008080;">38</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">const</span><span style="color: #000000;"> </span><span style="color: #0000ff;">string</span><span style="color: #000000;"> _defaultErrorMessage </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">请输入正确的{0}</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br /></span><span style="color: #008080;">39</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ValidateEmailAttribute() : </span><span style="color: #0000ff;">base</span><span style="color: #000000;">(_defaultErrorMessage) { }<br /></span><span style="color: #008080;">40</span> <span style="color: #000000;"><br /></span><span style="color: #008080;">41</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">override</span><span style="color: #000000;"> </span><span style="color: #0000ff;">string</span><span style="color: #000000;"> FormatErrorMessage(</span><span style="color: #0000ff;">string</span><span style="color: #000000;"> name)<br /></span><span style="color: #008080;">42</span> <span style="color: #000000;"> {<br /></span><span style="color: #008080;">43</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,<br /></span><span style="color: #008080;">44</span> <span style="color: #000000;"> name, _defaultErrorMessage);<br /></span><span style="color: #008080;">45</span> <span style="color: #000000;"> }<br /></span><span style="color: #008080;">46</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">override</span><span style="color: #000000;"> </span><span style="color: #0000ff;">bool</span><span style="color: #000000;"> IsValid(</span><span style="color: #0000ff;">object</span><span style="color: #000000;"> value)<br /></span><span style="color: #008080;">47</span> <span style="color: #000000;"> {<br /></span><span style="color: #008080;">48</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">string</span><span style="color: #000000;"> email </span><span style="color: #000000;">=</span><span style="color: #000000;"> value </span><span style="color: #0000ff;">as</span><span style="color: #000000;"> </span><span style="color: #0000ff;">string</span><span style="color: #000000;">;<br /></span><span style="color: #008080;">49</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (String.IsNullOrEmpty(email))<br /></span><span style="color: #008080;">50</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br /></span><span style="color: #008080;">51</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">string</span><span style="color: #000000;"> pattern </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\\.[a-zA-Z0-9_-])$</span><span style="color: #800000;">"</span><span style="color: #000000;">;<br /></span><span style="color: #008080;">52</span> <span style="color: #000000;"> Regex reg </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Regex(pattern);<br /></span><span style="color: #008080;">53</span> <span style="color: #000000;"> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> reg.IsMatch(email);<br /></span><span style="color: #008080;">54</span> <span style="color: #000000;"> }<br /></span><span style="color: #008080;">55</span> <span style="color: #000000;"> }<br /></span><span style="color: #008080;">56</span> <span style="color: #000000;">}<br /></span><span style="color: #008080;">57</span> </div></pre> </div> <p>&nbsp;</p> <br /></pre> <pre><pre>这几句为什么不会报错</pre> <pre> home.Name = null;</pre> <pre> home.Email = "asdfasdfasd";</pre> <pre>Console.WriteLine(home.Name+home.Email);</pre> <pre>请问是不是必需要基于MVC才行???</pre> <br /></pre>
问题补充: 你能就这个问题写的详细点吗 我刚涉及这个 不懂你说的 required特性不是存在于下面这个命名空间吗 怎么就不管用呢? using System.ComponentModel.DataAnnotations; 不过你说的对 我调测的时候的确进不了自定义特性中写的验证代码! 标签决定“验证的时候如何判断” 这句话是? 不理解啊 为什么会进不去自定义验证代码块呢
eofaieeiwf的主页 eofaieeiwf | 初学一级 | 园豆:105
提问于:2010-11-30 17:29
< >
分享
最佳答案
0

当然不会报错,拿MVC或者Entlib里的验证器去验证才会出错,标签决定“验证的时候如何判断”,你根本没有经过“验证”这一环节

Gray Zhang | 专家六级 |园豆:17610 | 2010-11-30 17:34
其他回答(1)
0

RequiredAttribute 只是一个简单标记,它不会主动去验证。

鹤冲天 | 园豆:2379 (老鸟四级) | 2010-11-30 21:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册